Class: WhittakerTech::Midas::Coin::Parser
- Inherits:
-
Object
- Object
- WhittakerTech::Midas::Coin::Parser
- Defined in:
- app/models/whittaker_tech/midas/coin/parser.rb
Overview
Parser coerces heterogeneous inputs into a Coin.
Accepted input types:
Accepted input types:
Coin: Returned as-is (no conversion)Money: Wrapsmoney.cents+money.currency.iso_codeinto a CoinNumeric: Treated as a major-unit amount;currency_coderequiredString: Strips non-numeric characters;currency_coderecommended
Numeric vs. Integer semantics
The Parser treats all Numeric inputs (including Integer) as major units
(dollars, euros, etc.) and scales them to minor units using
Money.from_amount. This differs from Coin#amount= and Coin.value,
which expect minor units directly.
If you already have cents use Coin.value(2999, 'USD') instead.
Class Method Summary collapse
-
.parse(value, currency_code: nil) ⇒ Coin
Parses a value into a
Coin.
Class Method Details
.parse(value, currency_code: nil) ⇒ Coin
Parses a value into a Coin.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/models/whittaker_tech/midas/coin/parser.rb', line 40 def parse(value, currency_code: nil) case value when WhittakerTech::Midas::Coin value when Money WhittakerTech::Midas::Coin.value(value.cents, value.currency.iso_code) when Numeric parse_numeric(value, currency_code) when String parse_string(value, currency_code) else raise TypeError, "Cannot convert #{value.class} to Coin" end end |