Class: Mint::MintMoneyType

Inherits:
ActiveRecord::Type::Value
  • Object
show all
Defined in:
lib/minting/money_attribute/money_type.rb

Overview

MintMoneyType

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(currency:) ⇒ MintMoneyType

Returns a new instance of MintMoneyType.



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

def initialize(currency:)
  @currency = Mint.currency currency
  super()
end

Class Method Details

.typeObject



33
34
35
# File 'lib/minting/money_attribute/money_type.rb', line 33

def self.type
  :mint_type
end

Instance Method Details

#assert_valid_value(value) ⇒ Object

Raises:

  • (ArgumentError)


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

def assert_valid_value(value)
  case value
  when Numeric, String
    return
  when Mint::Money
    return if value.currency == @currency

    message = "'#{value.inspect}' has different currency. Only #{@currency.code} allowed."
  else
    message = "'#{value}' is not a valid type for the attribute."
  end
  raise ArgumentError, message
end

#deserialize(value) ⇒ Object



25
26
27
# File 'lib/minting/money_attribute/money_type.rb', line 25

def deserialize(value)
  value && Mint.money(value, @currency)
end

#serialize(value) ⇒ Object



29
30
31
# File 'lib/minting/money_attribute/money_type.rb', line 29

def serialize(value)
  value.to_d
end