lib/money_helper.rb in money_helper-1.0.1 vs lib/money_helper.rb in money_helper-1.0.2
- old
+ new
@@ -1,17 +1,18 @@
-# encoding: UTF-8
+require_relative 'version'
require 'active_support/core_ext/object/blank'
require 'money'
module MoneyHelper
+ Money.locale_backend = :currency
I18n.enforce_available_locales = false
- SYMBOL_ONLY = %w{USD GBP EUR MYR} #don't use ISO code
- OK_SYMBOLS = %w{
+ SYMBOL_ONLY = %w[USD GBP EUR MYR].freeze # don't use ISO code
+ OK_SYMBOLS = %w[
$ £ € ¥ 元 р. L ƒ ৳ P R$ K ₡ D ლ ₵ Q G ₹ Rp ₪ ₩ ₭ R RM ₨ ₮ դր. C$ ₦ ₲ ₱ T ฿ T$ m ₴ ₫ ៛ ₺ E ₽
- } #ok to include in string
+ ].freeze # ok to include in string
##
# Formats a single amount in the given currency into a price string. Defaults to USD if currency not
# given.
#
@@ -25,28 +26,29 @@
# currency: (String)
# number_only: (Boolean) optional flag to exclude currency indicators (retains number formatting
# specific to currency)
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"
+
+ 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, symbol_position: :before, symbol: include_symbol }.merge(options)
- (number_only || SYMBOL_ONLY.include?(currency) ? "" : currency + " ") +
- Money.new(amount*subunit_factor.ceil, valid_currency).format(money_options).delete(' ')
+ 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
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)}"
+ iso_for_currency(currency).to_s
end
end
##
# Formats a low and high amount in the given currency into a price string
@@ -66,34 +68,34 @@
# delimiter: (String) optional
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)
+ 'Under ' + money_to_text(high, currency)
elsif high.blank?
- money_to_text(low, currency) + " and up"
+ 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)
+ [money_to_text(low, currency), money_to_text(high, currency, true)].compact.join(delimiter)
end
end
- private
-
def self.code_valid?(code)
Money::Currency.stringified_keys.include?(code.downcase)
end
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
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
end