lib/money_helper.rb in money_helper-2.0.0 vs lib/money_helper.rb in money_helper-3.0.0
- old
+ new
@@ -1,5 +1,7 @@
+# frozen_string_literal: true
+
require_relative 'version'
require 'active_support/core_ext/object/blank'
require 'money'
@@ -25,20 +27,19 @@
# = Arguments
#
# amount_minor: (Integer) amount in minor unit
# currency: (String) optional ISO currency code, defaulting to USD
# with_currency: (Boolean) optional flag to include ISO currency code, defaulting to true
- # with_symbol: (Boolean) optional flag to include currency symbol, defaulting to true
- def self.money_to_text(amount_minor, currency: 'USD', with_currency: true, with_symbol: true)
+ #  format: (Hash) optional formatting options to pass to `Money#format` e.g.:
+ # no_cents: (Boolean) optional flag to exclude cents, defaulting to false
+ # symbol: (Boolean) optional flag to include currency symbol, defaulting to true
+ def self.money_to_text(amount_minor, currency: 'USD', with_currency: true, format: {})
return '' if amount_minor.blank?
- money_options = {
- format: '%u%n',
- symbol: with_symbol
- }
+ format_options = { format: '%u%n' }.merge(format)
- formatted_amount = Money.new(amount_minor, currency).format(money_options)
+ formatted_amount = Money.new(amount_minor, currency).format(format_options)
formatted_currency = with_currency ? currency.upcase : ''
"#{formatted_currency} #{formatted_amount}".strip
end
@@ -71,17 +72,17 @@
# delimiter: (String) optional
def self.money_range_to_text(low, high, currency: 'USD', delimiter: ' - ')
if low.blank? && high.blank?
''
elsif low.blank?
- 'Under ' + money_to_text(high, currency: currency)
+ "Under #{money_to_text(high, currency: currency)}"
elsif high.blank?
- money_to_text(low, currency: currency) + ' and up'
+ "#{money_to_text(low, currency: currency)} and up"
elsif low == high
money_to_text(low, currency: currency)
else
formatted_low = money_to_text(low, currency: currency)
- formatted_high = money_to_text(high, currency: currency, with_currency: false, with_symbol: false)
+ formatted_high = money_to_text(high, currency: currency, with_currency: false, format: { symbol: false })
[formatted_low, formatted_high].compact.join(delimiter)
end
end
def self.code_valid?(code)