Class: G2L::Input
- Inherits:
-
Object
- Object
- G2L::Input
- Defined in:
- lib/input.rb
Instance Attribute Summary collapse
-
#input ⇒ Object
Returns the value of attribute input.
Instance Method Summary collapse
-
#initialize(input) ⇒ Input
constructor
A new instance of Input.
- #to_ledger ⇒ Object
Constructor Details
#initialize(input) ⇒ Input
Returns a new instance of Input.
9 10 11 |
# File 'lib/input.rb', line 9 def initialize(input) self.input = input end |
Instance Attribute Details
#input ⇒ Object
Returns the value of attribute input.
7 8 9 |
# File 'lib/input.rb', line 7 def input @input end |
Instance Method Details
#to_ledger ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/input.rb', line 13 def to_ledger doc = Nokogiri::XML(input) accounts = doc.xpath('//gnc:account').inject({}) do |all, account| id = account.xpath("act:id")[0].text parent = account.xpath("act:parent")[0] all.update(id => { :name => account.xpath("act:name")[0].text, :commodity => account.xpath("act:commodity/cmdty:id")[0].try(:text), :parent => parent ? parent.text : nil }) end accounts = resolve_accounts(accounts) transactions = doc.xpath('//gnc:transaction').map do |transaction| { :date => Date.parse(transaction.xpath("trn:date-posted/ts:date")[0].text), :description => transaction.xpath("trn:description")[0].text, :splits => transaction.xpath("trn:splits/trn:split").map {|split| { :account => split.xpath("split:account")[0].text, :action => split.xpath("split:action")[0].try(:text), :value => parse_value(split.xpath("split:value")[0]), :quantity => parse_value(split.xpath("split:quantity")[0]), :reconciled => split.xpath("split:reconciled-state")[0].text == 'y' }} } end # Generate output transactions.sort_by {|x| x[:date] }.map do |tx| (["%s %s%s" % [ tx[:date].strftime("%Y/%m/%d"), tx[:splits].any? {|y| y[:reconciled] } ? '* ' : '', tx[:description] ]] + tx[:splits].map {|split| account = accounts[split[:account]] value = if split[:action] "%i %s" % [split[:quantity].to_i, account[:commodity]] else '$' + split[:quantity].to_s end " %-44s%s" % [account[:name], value] }).join("\n") end.join("\n\n") end |