Module: ActsAsMoney::ClassMethods

Defined in:
lib/acts_as_money.rb

Instance Method Summary collapse

Instance Method Details

#money(name, options = {}) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/acts_as_money.rb', line 19

def money(name, options = {})
  mapping = [[(options[:cents] || :cents).to_s, 'cents']]
  mapping << [(options[:currency] || :currency).to_s, 'currency'] if options[:currency] != false
  composed_of name,
              class_name: 'Money',
              allow_nil: options[:allow_nil],
              mapping: mapping,
              constructor: proc { |cents, currency| options[:allow_nil] && !cents ? nil : Money.new(cents || 0, currency || Money.default_currency) },
              converter: proc { |value|
                case value
                when Integer
                  Money.new(value, Money.default_currency)
                when Float
                  Money.new((value * 100).to_d, Money.default_currency)
                when String
                  Money.new((value.to_f * 100).to_d, Money.default_currency)
                else
                  value
                end
              }
end