RubyMoney / monetize

A library for converting various objects into `Money` objects.
MIT License
430 stars 107 forks source link

Version 1.11.0 broke parsing for Malaysian ringgit when using the symbol 'RM' #153

Closed goalaleo closed 1 year ago

goalaleo commented 3 years ago

Malaysian ringgit, ISO code: MYR, symbol: RM. The commit that broke our code is this one which prioritises ISO code over a symbol. It makes the incorrect assumption that the first line only matches ISO codes: https://github.com/RubyMoney/monetize/blob/a0680812444181cb8eb6b1d3a288b9dbe8934d24/lib/monetize/parser.rb#L72-L73

While it's true that the regexp doesn't match any symbols in CURRENCY_SYMBOLS, there are a few currencies where the symbol is 2-3 capital letters

Using version 1.9.4

Monetize::Parser::CURRENCY_SYMBOLS["RM"] = "MYR"
Monetize.parse("100 RM")
=> #<Money fractional:10000 currency:MYR>

In version 1.11.0, expected same result, got

=> nil

I propose that if

computed_currency = input[/[A-Z]{2,3}/]

returns a match, the code will check if CURRENCY_SYMBOLS contains it as a key before assuming that it's an ISO code.

As a side note: what is the recommended way of "registering" symbols that are not included in the gem? In our app we've added them to the Monetize::Parser::CURRENCY_SYMBOLS hash, but maybe this is not recommended?

semmons99 commented 3 years ago

@antstorm I feel this is a regression and we should accept a PR, agree?

As for your side note. We do not have an officially supported way of adding symbols. I would suggest monkey patching the constant as you have done, knowing you will need to ensure it still works after each release. An alternative would be to submit a patch that adds it as an official feature.