Skip to content

Commit

Permalink
Merge pull request #65 from michaelpreston/feature/parse-money-object
Browse files Browse the repository at this point in the history
Parsing a Money object should return it unchanged
  • Loading branch information
antstorm committed May 29, 2016
2 parents 7a55771 + 4de6016 commit 7883071
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@
- Reformat code to adapt to Rubocop guidelines
- Add config setting to always enforce currency delimiters
- Add rake console task
- Fix issue where parsing a Money object resulted in a Money object with its currency set to `Money.default_currency`,
rather than the currency that it was sent in as.
2 changes: 2 additions & 0 deletions lib/monetize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class << self
end

def self.parse(input, currency = Money.default_currency, options = {})
return input if input.class == Money

input = input.to_s.strip

computed_currency = if options.fetch(:assume_from_symbol) { assume_from_symbol }
Expand Down
10 changes: 10 additions & 0 deletions spec/monetize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@
Monetize.enforce_currency_delimiters = false
end

context 'Money object attempting to be parsed' do
let(:money) { Money.new(595, 'GBP') }

it 'returns the original Money object' do
expect(Monetize.parse(money)).to eq money
expect(Monetize.parse(money).currency).to eq 'GBP'
expect(Monetize.parse(money).cents).to eq 595
end
end

context 'custom currencies with 4 decimal places' do
before :each do
Money::Currency.register(JSON.parse(bar, symbolize_names: true))
Expand Down

0 comments on commit 7883071

Please sign in to comment.