Class: Money::Parser::Simple
- Inherits:
-
Object
- Object
- Money::Parser::Simple
- Defined in:
- lib/money/parser/simple.rb
Constant Summary collapse
- SIGNED_DECIMAL_MATCHER =
/\A-?\d*(?:\.\d*)?\z/.freeze
Class Method Summary collapse
-
.parse(input, currency, strict: false) ⇒ Money?
Parses an input string using BigDecimal, it always expects a dot character as a decimal separator and generally does not accept other characters other than minus-hyphen and digits.
Class Method Details
.parse(input, currency, strict: false) ⇒ Money?
Parses an input string using BigDecimal, it always expects a dot character as a decimal separator and generally does not accept other characters other than minus-hyphen and digits. It is useful for APIs, interop with other languages and other use cases where you expect well-formatted input and do not need to take user locale into consideration.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/money/parser/simple.rb', line 16 def parse(input, currency, strict: false) currency = Money::Helpers.value_to_currency(currency) return unless currency coerced = input.to_s if SIGNED_DECIMAL_MATCHER.match?(coerced) && (amount = BigDecimal(coerced, exception: false)) Money.new(amount, currency) elsif strict raise ArgumentError, "unable to parse input=\"#{input}\" currency=\"#{currency}\"" end end |