diff --git a/lib/ledger.rb b/lib/ledger.rb index 4fd49afd..b7a21e3a 100644 --- a/lib/ledger.rb +++ b/lib/ledger.rb @@ -50,6 +50,20 @@ module Ledger end end + def balance( cleared = false, depth = nil, period = nil, categories = '' ) + period = period.nil? ? '' : "-p '#{period}'" + depth = depth.nil? ? '' : "--depth #{depth}" + operation = cleared ? 'cleared' : 'balance' + run( "--flat --no-total --exchange '#{CURRENCY}' #{period} #{depth}", operation, categories ) + .split( "\n" ) + .map do |line| + line_array = line.split( "#{CURRENCY}" ) + + { account: line_array[ 1 ].strip, + amount: line_array[ 0 ].tr( SEPARATOR, '.' ).to_f } + end + end + def cleared run( "--flat --no-total --exchange '#{CURRENCY}'", 'cleared', 'Assets Equity' ) .split( "\n" )