Skip to content

Class WhittakerTech::Midas::Coin::Parser

Inherits: Object

Parser coerces heterogeneous inputs into a Coin.

Accepted input types:

Accepted input types:

  • Coin: Returned as-is (no conversion)
  • Money: Wraps money.cents + money.currency.iso_code into a Coin
  • Numeric: Treated as a major-unit amount; currency_code required
  • String: Strips non-numeric characters; currency_code recommended

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.

  • @since 0.1.0

@example

Coin.parse(Money.new(2999, 'USD'))          # => Coin(2999 USD)
Coin.parse(29.99, currency_code: 'USD')     # => Coin(2999 USD)
Coin.parse('$29.99')                        # => Coin(2999 USD)  (uses Money.default_currency)
Coin.parse('29.99', currency_code: 'USD')   # => Coin(2999 USD)

Public Class Methods

parse(value, currency_code: = nil)

Parses a value into a Coin.

  • @param value [Coin, Money, Numeric, String] the value to parse
  • @param currency_code [String, nil] required for bare Numeric and plain String values

  • @raise [TypeError] if value is an unsupported type

  • @raise [ArgumentError] if currency_code is required but not provided
  • @return [Coin]
  • @since 0.1.0