Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated the sources to allow for custom curreny #11

Merged
merged 2 commits into from Jun 26, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion lib/reckon/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ def initialize(options = {})
self.tokens = {}
self.accounts = {}
self.seen = {}

unless options[:currency]
options[:currency] = '$'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unless/end block could be written as:

options[:currency] ||= '$'

end

unless options[:suffixed]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably not needed since options[:suffixed] will be nil, which evaluates to false in any if statements.

options[:suffixed] = false
end

learn!
parse
filter_csv
Expand Down Expand Up @@ -168,7 +177,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 +417,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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if the . in the character class needs to be escaped here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm.. I based that on this test

irb(main):005:0> money = "10.0 SEK"
=> "10.0 SEK"
irb(main):006:0> money.gsub(/[^0-9-]/, '').to_f
=> 100.0
money.gsub(/[^0-9.-]/, '').to_f
=> 10.0

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, looks good. :)

On Tue, Jun 25, 2013 at 10:15 PM, Sebastian Hörberg <
notifications@github.com> wrote:

In lib/reckon/ledger_parser.rb:

@@ -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
    

Hm.. I based that on this test

irb(main):005:0> money = "10.0 SEK"=> "10.0 SEK"irb(main):006:0> money.gsub(/[^0-9-]/, '').to_f=> 100.0money.gsub(/[^0-9.-]/, '').to_f=> 10.0


Reply to this email directly or view it on GitHub/~https://github.com//pull/11/files#r4884192
.

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