Class: Monetize::Parser
- Inherits:
-
Object
- Object
- Monetize::Parser
- Defined in:
- lib/monetize/parser.rb
Constant Summary collapse
- CURRENCY_SYMBOLS =
{ '$' => 'USD', '€' => 'EUR', '£' => 'GBP', '₤' => 'GBP', 'R$' => 'BRL', 'RM' => 'MYR', 'Rp' => 'IDR', 'R' => 'ZAR', '¥' => 'JPY', 'C$' => 'CAD', '₼' => 'AZN', '元' => 'CNY', 'Kč' => 'CZK', 'Ft' => 'HUF', '₹' => 'INR', '₽' => 'RUB', '₺' => 'TRY', '₴' => 'UAH', 'Fr' => 'CHF', 'zł' => 'PLN', '₸' => 'KZT', "₩" => 'KRW', 'S$' => 'SGD', 'HK$'=> 'HKD', 'NT$'=> 'TWD', '₱' => 'PHP', }
- CURRENCY_SYMBOL_REGEX =
/(?<![A-Z])(#{CURRENCY_SYMBOLS.keys.map { |key| Regexp.escape(key) }.join('|')})(?![A-Z])/i- MULTIPLIER_SUFFIXES =
{ 'K' => 3, 'M' => 6, 'B' => 9, 'T' => 12 }
- MULTIPLIER_REGEXP =
Regexp.new(format('^(.*?\d)(%s)\b([^\d]*)$', MULTIPLIER_SUFFIXES.keys.join('|')), 'i')
- DEFAULT_DECIMAL_MARK =
'.'.freeze
Instance Method Summary collapse
-
#initialize(input, fallback_currency = Money.default_currency, options = {}) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
Constructor Details
#initialize(input, fallback_currency = Money.default_currency, options = {}) ⇒ Parser
Returns a new instance of Parser.
41 42 43 44 45 |
# File 'lib/monetize/parser.rb', line 41 def initialize(input, fallback_currency = Money.default_currency, = {}) @input = input.to_s.strip @fallback_currency = fallback_currency @options = end |
Instance Method Details
#parse ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/monetize/parser.rb', line 47 def parse multiplier_exp, input = extract_multiplier num = input.gsub(/(?:^#{currency.symbol}|[^\d.,'-]+)/, '') negative, num = extract_sign(num) amount = to_big_decimal(normalize_number(num)) amount = apply_multiplier(multiplier_exp, amount) amount = apply_sign(negative, amount) [amount, currency] end |