Class: Money::Parser::LocaleAware
- Inherits:
-
Object
- Object
- Money::Parser::LocaleAware
- Defined in:
- lib/money/parser/locale_aware.rb
Class Attribute Summary collapse
-
.decimal_separator_resolver ⇒ Object
The
Proccalled to get the current locale decimal separator.
Class Method Summary collapse
-
.parse(input, currency, strict: false, decimal_separator: decimal_separator_resolver&.call) ⇒ Money?
Parses an input string, normalizing some non-ASCII characters to their equivalent ASCII, then discarding any character that is not a digit, hyphen-minus or the decimal separator.
Class Attribute Details
.decimal_separator_resolver ⇒ Object
The Proc called to get the current locale decimal separator. In Rails apps this defaults to the same lookup ActionView’s number_to_currency helper will use to format the monetary amount for display.
11 12 13 |
# File 'lib/money/parser/locale_aware.rb', line 11 def decimal_separator_resolver @decimal_separator_resolver end |
Class Method Details
.parse(input, currency, strict: false, decimal_separator: decimal_separator_resolver&.call) ⇒ Money?
Parses an input string, normalizing some non-ASCII characters to their equivalent ASCII, then discarding any character that is not a digit, hyphen-minus or the decimal separator. To prevent user confusion, make sure that formatted Money strings can be parsed back into equivalent Money objects.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/money/parser/locale_aware.rb', line 29 def parse(input, currency, strict: false, decimal_separator: decimal_separator_resolver&.call) raise ArgumentError, "decimal separator cannot be nil" unless decimal_separator currency = Money::Helpers.value_to_currency(currency) return unless currency normalized_input = input .tr('-0-9.,、、', '-0-9.,,,') .gsub(/[^\d\-#{Regexp.escape(decimal_separator)}]/, '') .gsub(decimal_separator, '.') amount = BigDecimal(normalized_input, exception: false) if amount Money.new(amount, currency) elsif strict raise ArgumentError, "unable to parse input=\"#{input}\" currency=\"#{currency}\"" end end |