ledgerrb/lib/ledger.rb

48 lines
1,007 B
Ruby
Raw Normal View History

# encoding: utf-8
2014-07-14 16:18:41 +02:00
# Ruby wrapper module for calling ledger
module Ledger
module_function
@binary = 'ledger'
@file = '~/org/comptes.ledger'
def run( options, command = '', command_parameters = '' )
2014-07-14 16:18:22 +02:00
`#{@binary} -f #{@file} #{options} #{command} #{command_parameters}`
end
def version
run '--version'
end
def accounts( depth = 9999 )
run( '', 'accounts' )
.split( "\n" )
2014-07-14 16:18:41 +02:00
.map do |a|
a.split( ':' )
.each_slice( depth )
.to_a.first
end.uniq
end
def cleared
2014-07-14 16:18:22 +02:00
run '', 'cleared'
end
def monthly_register( category )
2014-07-14 16:18:22 +02:00
run( "--monthly --collapse --amount-data --exchange '€'", 'register', "#{category}" )
.split( "\n" )
2014-07-14 16:18:41 +02:00
.map do |line|
line_array = line.split
2014-07-14 16:18:41 +02:00
{ date: line_array[ 0 ],
amount: line_array[ 1 ].to_f }
end
end
def balance( period = nil )
period = period.nil? ? '' : "-p #{period}"
2014-07-14 16:18:22 +02:00
output run "--flat --exchange '€' #{period}", 'balance', "#{category}"
end
end