Module: MoneyHelper

Defined in:
lib/version.rb,
lib/money_helper.rb

Constant Summary collapse

VERSION =
'3.0.2'
SYMBOL_ONLY =

don’t use ISO code

%w[USD GBP EUR MYR].freeze
OK_SYMBOLS =
%w[
  $ £  ¥  р. L ƒ  P R$ K  D   Q G  Rp    R RM   դր. C$    T ฿ T$ m     E 
].freeze

Class Method Summary collapse

Class Method Details

.code_valid?(code) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/money_helper.rb', line 93

def self.code_valid?(code)
  Money::Currency.stringified_keys.include?(code.downcase)
end

.iso_for_currency(code) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/money_helper.rb', line 97

def self.iso_for_currency(code)
  return unless code && code_valid?(code)

  Money::Currency.new(code).iso_code.tap do |iso_code|
    iso_code.strip! if iso_code.present?
  end
end

.money_range_to_text(low, high, currency: 'USD', delimiter: ' - ', with_currency: true, format: {}) ⇒ Object

Formats a low and high amount in the given currency into a price string

Example

MoneyHelper.money_range_to_text(30_175_93, 40_983_27, currency: 'USD')     #=> 'USD $30,175.93 - 40,983.27'
MoneyHelper.money_range_to_text(30_175_93, 40_983_27)                      #=> 'USD $30,175.93 - 40,983.27'
MoneyHelper.money_range_to_text(30_175_93, 40_983_27, currency: 'EUR')     #=> 'EUR €30.175,93 - 40.983,27'
MoneyHelper.money_range_to_text(30_175_93, 40_983_27, delimiter: ' ... ')  #=> 'USD $30,175.93 ... 40,983.27'

Arguments

low: (Integer) amount in minor unit
high: (Integer) amount in minor unit
currency: (String) optional ISO currency code, defaulting to USD
delimiter: (String) optional
with_currency: (Boolean) optional flag to include ISO currency code, defaulting to 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


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/money_helper.rb', line 77

def self.money_range_to_text(low, high, currency: 'USD', delimiter: ' - ', with_currency: true, format: {})
  if low.blank? && high.blank?
    ''
  elsif low.blank?
    "Under #{money_to_text(high, currency: currency, with_currency: with_currency, format: format)}"
  elsif high.blank?
    "#{money_to_text(low, currency: currency, with_currency: with_currency, format: format)} and up"
  elsif low == high
    money_to_text(low, currency: currency, with_currency: with_currency, format: format)
  else
    formatted_low = money_to_text(low, currency: currency, with_currency: with_currency, format: format)
    formatted_high = money_to_text(high, currency: currency, with_currency: false, format: { symbol: false }.merge(format))
    [formatted_low, formatted_high].compact.join(delimiter)
  end
end

.money_to_text(amount_minor, currency: 'USD', with_currency: true, format: {}) ⇒ Object

Formats a single amount in the given currency into a price string. Defaults to USD if currency not

given.

Example

MoneyHelper.money_to_text(30_175_93, currency: 'USD')  #=> 'USD $30,175.93'
MoneyHelper.money_to_text(30_175_93, currency: 'EUR')  #=> 'EUR €30.175,93'
MoneyHelper.money_to_text(30_175_93)                   #=> 'USD $30,175.93'

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

 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


35
36
37
38
39
40
41
42
43
44
# File 'lib/money_helper.rb', line 35

def self.money_to_text(amount_minor, currency: 'USD', with_currency: true, format: {})
  return '' if amount_minor.blank?

  format_options = { format: '%u%n' }.merge(format)

  formatted_amount = Money.new(amount_minor, currency).format(format_options)
  formatted_currency = with_currency ? currency.upcase : ''

  "#{formatted_currency} #{formatted_amount}".strip
end

.symbol_for_code(code) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/money_helper.rb', line 105

def self.symbol_for_code(code)
  return unless code && code_valid?(code)

  Money::Currency.new(code).symbol.tap do |symbol|
    symbol.strip! if symbol.present?
  end
end

.symbol_with_optional_iso_code(currency = 'USD') ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/money_helper.rb', line 46

def self.symbol_with_optional_iso_code(currency = 'USD')
  symbol = symbol_for_code(currency)
  if SYMBOL_ONLY.include?(currency)
    symbol
  elsif symbol && OK_SYMBOLS.include?(symbol)
    "#{iso_for_currency(currency)} #{symbol}"
  else
    iso_for_currency(currency).to_s
  end
end