-
Notifications
You must be signed in to change notification settings - Fork 113
Parse currency correctly if found from existing hash #167
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
Parse currency correctly if found from existing hash #167
Conversation
Hi @semmons99 , would you able to take a look at this PR? |
ty. merged! |
Hi @semmons99 thanks for merging, is it possible to bump the version for this gem in both rubygems and create a new version tag in Github ? |
done. release 1.13.0 @kahshing96 |
@kahshing96 @semmons99 That's a massive breaking change considering the limited set of CURRENCY_SYMBOLS. # monetize 0.12.0
'100 NOK'.to_money => #<Money fractional:10000 currency:NOK>
# monetize 0.13.0 (with EUR as default)
'100 NOK'.to_money => #<Money fractional:10000 currency:EUR> |
Just noticed this change when writing Yes, this is a breaking change and should not belong to 1.x, however, I'm wondering why this is not using ISO codes from Money ( |
Yes, this is breaking our production because fee = "AUD 10.00"
# Our default currency is "USD"
Monetize.parse(fee)
# => #<Money fractional:1000 currency:USD>
# The result is in USD instead of AUD |
def parse_currency | ||
computed_currency = nil | ||
computed_currency = input[/[A-Z]{2,3}/] | ||
computed_currency = nil unless Monetize::Parser::CURRENCY_SYMBOLS.value?(computed_currency) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(time passed, a legacy project upgraded the gem, weirdness ensued 😄 )
ok, this line is kinda weird as it's nullifying the computed_currency
based on a very tiny list of known currencies and not, say, the entire Money::Currency
list
for example, we had a bunch of tests that specifically tested Australian Dollars parsing, as in
Monetize.parse('100 AUD')
AUD
is known to Money
, so that should be fine
Money::Currency.new('AUD')
=> #<Money::Currency id: aud, priority: 4, symbol_first: true, thousands_separator: ,, html_entity: $, decimal_mark: ., name: Australian Dollar, symbol: $, subunit_to_unit: 100, exponent: 2, iso_code: AUD, iso_numeric: 036, subunit: Cent, smallest_denomination: 5, format: >
but because of this line and because it's not on the subset of currencies Monetize
knows about, it's now defaulting to USD and I'm kinda struggling with a reason why this would be good, especially when the result is used in Money::Currency.wrap(parse_currency)
What's worse - in my case - is that there's no way around it, if there's a chance that we'll use a valid currency that unfortunately is not on the subset of Monetize
known currencies, it'll end up being nillified
is there any context to this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is likely simply an oversight. Because there is a smaller subset of known symbols than known currencies, it is unfortunately gating them down too far. I think the better solution would be to try and match on symbol from this hash and then use the much larger known currencies iso codes to figure out the rest. we would be happy to accept and merge a PR that did it that way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I understand the need to have a list for "our pick of currency for a given symbol" -- there are many currencies with dollar signs in them, so that one I can follow, the other way around I couldn't understand , but your explanation makes sense :)
I might take a stab at a PR for this, keeping the lookup for symbols as is but relying on Money::Currency
to figure out if the named currency is known (and nullifying if not, which I think was the real intention here)
computed_currency
parsed from the given input from a match ISO currency code regex is a correct ISO code from existingMonetize::Parser::CURRENCY_SYMBOLS
, then only assigned it with valuecompute_currency
which match against currency symbol regex