Skip to content
printercu edited this page Dec 27, 2016 · 3 revisions

money2 gem

money2 is the successor of money. Main purpose of this fork is to simplify entire codebase and remove legacy.

There is PR in money gem, but it's awaiting for the review for too long. I'm going to release it as money2 gem, so it'll be available on rubygems, and more developers can give it a try.

Just change gem 'money' -> gem 'money2' in your Gemfile.

Three main points:

  • It uses BigDecimal to store amounts. More intuitive Money.new(1.23, :usd) or Money.new('1.23', :usd) instead of Money.new(123, :usd) or Money.from_amount(1.23, :usd). This also allowed to remove a lot of conversion stuff all over the code.
  • Formatting stuff is moved to Formatter object, which is improved a lot. This allows to implement and plug-in custom formatters. For example, #to_s is implemented with separate formatter.
  • Simplified arithmetics. No more implicit conversions with edge-cases: use #to_d to compare with numbers, and put numbers as a second argument for arithmetic operations. This allows to remove all the checks for edge-cases (like division number by money, comparison with zeroes, etc).

Of course, it brings backward incompatibility. Here are compatibility modules which I used while migrating gem's code base. If you have good test coverage, just enable them all, and disable one by one.

All changes:

  • Moved lib/money/money/* -> lib/money.
  • Formatting specific methods moved to Formatter.
    • default_formatting_rules & use_i18n moved to Formatter.
    • Formatter.default_rules= doesn't support nil (hash must be given).
  • Remove #currency_as_string. Changelog says it's for rails, but there is no such calls in rails's master.
  • Lambda passed to .default_currency must return currency object (not symbol).
  • Rename Currency#iso_code -> #code
  • Set Currency#code same as id (YEN to get JPY, and GHC for GHS will not be supported)
  • Improved and cleaned up default formatter (2x faster) & #to_s.
    • changed empty Currency.html_entity to nil
    • renamed options, dropped fallbacks:
      • south_asian_number_formatting => south_asian
      • translate => translate_symbol
      • thousands_separator => delimiter
      • decimal_mark => separator
      • symbol_after_without_space, symbol_before_without_space => symbol_space
  • Simplify arithmetics (encourage using to_d).
  • Use whole amounts (not cents) as main value.
    • Rounding block to Bank#exchange_with receives decimal amount and currency so it must be changed to ->(value, currency) { value.round(currency.decimal_places) }.
  • Don't depend on sixarm_ruby_unaccent. Add it to your Gemfile if you use Currency.analyze.
  • Currency.keep_only to cleanup currencies.
Clone this wiki locally