Module: MoneyHelper

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

Constant Summary collapse

VERSION =
'1.0.2'.freeze
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)


83
84
85
# File 'lib/money_helper.rb', line 83

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

.iso_for_currency(code) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/money_helper.rb', line 87

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, delimiter = ' - ') ⇒ Object

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

Example

$10,000 - 20,000 for (10000, 20000, "USD")
HKD $10,000 - 20,000 for (10000, 20000, "HKD")
$10,000 ... 20,000 for (10000, 20000, "USD", " ... ")
HKD $10,000 ... 20,000 for (10000, 20000, "HKD", " ... ")

Arguments

low: (Float)
high: (Float)
currency: (String)
delimiter: (String) optional


69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/money_helper.rb', line 69

def self.money_range_to_text(low, high, currency, delimiter = ' - ')
  if low.blank? && high.blank?
    nil
  elsif low.blank?
    'Under ' + money_to_text(high, currency)
  elsif high.blank?
    money_to_text(low, currency) + ' and up'
  elsif low == high
    money_to_text(low, currency)
  else
    [money_to_text(low, currency), money_to_text(high, currency, true)].compact.join(delimiter)
  end
end

.money_to_text(amount, currency, number_only = false, options = {}) ⇒ Object

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

given.

Example

$10,000; HKD $10,000 for (10000, "USD") and (10000, "HKD"), respectively

Arguments

amount: (Float)
currency: (String)
number_only: (Boolean) optional flag to exclude currency indicators (retains number formatting
  specific to currency)


29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/money_helper.rb', line 29

def self.money_to_text(amount, currency, number_only = false, options = {})
  return nil unless amount.present?

  currency = 'USD' if currency.blank?
  valid_currency = code_valid?(currency) ? currency : 'USD'
  symbol = symbol_for_code(currency)
  include_symbol = !number_only && symbol.present? && OK_SYMBOLS.include?(symbol)
  subunit_factor = Money::Currency.new(valid_currency).subunit_to_unit
  money_options = { no_cents: true, format: '%u %n', symbol: include_symbol }.merge(options)
  (number_only || SYMBOL_ONLY.include?(currency) ? '' : currency + ' ') +
    Money.new(amount * subunit_factor.ceil, valid_currency).format(money_options).delete(' ')
end

.symbol_for_code(code) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/money_helper.rb', line 95

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



42
43
44
45
46
47
48
49
50
51
# File 'lib/money_helper.rb', line 42

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