Added an Account#path method to get the path of an account, from root to literal...
[fin.git] / tabdelimited.rb
blob7ba7dd8bae3304a9a858253155e69580e449ace5
1 require 'transaction'
2 require 'accountregister'
4 ################################################################################
5 # Exports transactions to and imports transactions from tab-delimited input.
6 ################################################################################
7 module TabDelimited
9   def import
10     File.open(@filename) do |file|
11       file.each_line do |line|
12         line.chomp!
13         next if line =~ /^#|^$/ # ignore blank, comment lines
14         add(import_transaction(line))
15       end
16     end
17   end
19   def export
20     File.open(@filename, "w") do |file|
21       each do |transaction|
22         exported_transaction = [
23                                 transaction.from,
24                                 transaction.to,
25                                 transaction.amount,
26                                 transaction.date,
27                                 transaction.description
28                                ].join("\t")
29         file.puts(exported_transaction)
30       end
31     end
32   end
34   private
35   def import_transaction(line)
36     transaction_values = line.split(/\t/, -1) # [from, to, amount, date, desc]
37     from, to, amount, date, description = transaction_values
38     from = @account_register.open(*from.split('.'))
39     to = @account_register.open(*to.split('.'))
40     amount = amount.to_f
41     date = Date.parse(date)
42     Transaction.new(from, to, amount, date, description)
43   end
45   def export_transaction(transaction)
46   end
47 end