Skip to content

Commit

Permalink
Merge pull request #11 from zebh/currencies
Browse files Browse the repository at this point in the history
Updated the sources to allow for custom curreny
  • Loading branch information
cantino committed Jun 26, 2013
2 parents 6ae7130 + 9e304ea commit 5506b21
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
16 changes: 15 additions & 1 deletion lib/reckon/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def initialize(options = {})
self.tokens = {}
self.accounts = {}
self.seen = {}
self.options[:currency] ||= '$'
learn!
parse
filter_csv
Expand Down Expand Up @@ -168,7 +169,12 @@ def pretty_money_for(index, negate = false)
end

def pretty_money(amount, negate = false)
(amount >= 0 ? " " : "") + sprintf("%0.2f", amount * (negate ? -1 : 1)).gsub(/^((\-)|)(?=\d)/, '\1$')
currency = options[:currency]
if options[:suffixed]
(amount >= 0 ? " " : "") + sprintf("%0.2f #{currency}", amount * (negate ? -1 : 1))
else
(amount >= 0 ? " " : "") + sprintf("%0.2f", amount * (negate ? -1 : 1)).gsub(/^((\-)|)(?=\d)/, "\\1#{currency}")
end
end

def date_for(index)
Expand Down Expand Up @@ -403,6 +409,14 @@ def self.parse_opts(args = ARGV)
options[:encoding] = e
end

opts.on("-c", "--currency", "Currency symbol to use, defaults to $. ex.) $, EUR") do |e|
options[:currency] = e
end

opts.on("", "--suffixed", "If --currency should be used as a suffix. Defaults to false.") do |e|
options[:suffixed] = e
end

opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
Expand Down
2 changes: 1 addition & 1 deletion lib/reckon/ledger_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def balance(accounts)

def clean_money(money)
return nil if money.nil? || money.length == 0
money.gsub(/[\$,]/, '').to_f
money.gsub(/[^0-9.-]/, '').to_f
end
end
end
19 changes: 19 additions & 0 deletions spec/reckon/app_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# encoding: utf-8

require "spec_helper"
require 'rubygems'
Expand Down Expand Up @@ -158,6 +159,24 @@
@some_other_bank.pretty_money_for(5).should == " $0.23"
@some_other_bank.pretty_money_for(6).should == "-$0.96"
end

it "work with other currencies such as €" do
euro_bank = Reckon::App.new(:string => SOME_OTHER_CSV, :currency => "€", :suffixed => false )
euro_bank.pretty_money_for(1).should == "-€20.00"
euro_bank.pretty_money_for(4).should == " €1558.52"
euro_bank.pretty_money_for(7).should == "-€116.22"
euro_bank.pretty_money_for(5).should == " €0.23"
euro_bank.pretty_money_for(6).should == "-€0.96"
end

it "work with suffixed currencies such as SEK" do
swedish_bank = Reckon::App.new(:string => SOME_OTHER_CSV, :currency => 'SEK', :suffixed => true )
swedish_bank.pretty_money_for(1).should == "-20.00 SEK"
swedish_bank.pretty_money_for(4).should == " 1558.52 SEK"
swedish_bank.pretty_money_for(7).should == "-116.22 SEK"
swedish_bank.pretty_money_for(5).should == " 0.23 SEK"
swedish_bank.pretty_money_for(6).should == "-0.96 SEK"
end
end

describe "merge_columns" do
Expand Down
11 changes: 10 additions & 1 deletion spec/reckon/ledger_parser_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
#encoding: utf-8

require "spec_helper"
require 'rubygems'
Expand All @@ -12,7 +13,7 @@

describe "parse" do
it "should ignore non-standard entries" do
@ledger.entries.length.should == 5
@ledger.entries.length.should == 7
end

it "should parse entries correctly" do
Expand Down Expand Up @@ -76,6 +77,14 @@
Assets:Bank:Checking $1,000.00
Equity:Opening Balances
2004-05-01 * Checking balance
Assets:Bank:Checking €1,000.00
Equity:Opening Balances
2004-05-01 * Checking balance
Assets:Bank:Checking 1,000.00 SEK
Equity:Opening Balances
2004/05/01 * Investment balance
Assets:Brokerage 50 AAPL @ $30.00
Equity:Opening Balances
Expand Down

0 comments on commit 5506b21

Please sign in to comment.