Class: Mint::MoneyAttribute::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/minting/money_attribute/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(currency = Mint.default_currency) ⇒ Parser

Returns a new instance of Parser.



7
8
9
# File 'lib/minting/money_attribute/parser.rb', line 7

def initialize(currency = Mint.default_currency)
  @default_currency = currency
end

Instance Method Details

#parse(amount, currency = @default_currency) ⇒ Object Also known as: call



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/minting/money_attribute/parser.rb', line 11

def parse(amount, currency = @default_currency)
  currency = Mint.assert_valid_currency!(currency)
  case amount
  when NilClass    then nil
  when Numeric     then Mint::Money.create(amount, currency)
  when String      then Mint::Money.create(amount.to_r, currency)
  when Mint::Money
   return amount if amount.currency == currency
   raise TypeError, "Cannot automatically convert #{amount} to #{currency.code}"
  else
    Mint.parse(amount, currency)
  end
end