Module: ActsAsMoney::ClassMethods

Defined in:
lib/acts_as_money.rb

Instance Method Summary collapse

Instance Method Details

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



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

def money(name, options = {})
  mapping = [[(options[:cents] || :cents).to_s, 'cents']]
  mapping << [(options[:currency] || :currency).to_s, 'currency_as_string'] if options[:currency] != false
  composed_of name,  {
    :class_name => 'Money',
    :allow_nil => options[:allow_nil],
    :mapping => mapping,
    :constructor => Proc.new {|cents, currency| options[:allow_nil] && !cents ? nil : Money.new(cents || 0, currency || Money.default_currency)},
    :converter => Proc.new {  |value|
      case value
      when Fixnum
        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