Module: MoneyColumn::StoresMoney::ClassMethods

Defined in:
lib/money_column/stores_money.rb

Instance Method Summary collapse

Instance Method Details

#stores_money(money_name, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/money_column/stores_money.rb', line 8

def stores_money(money_name, options = {})
  options.reverse_merge!({
    :cents_attribute => "#{money_name}_in_cents",
    :allow_nil => true
  })

  class_eval "    def \#{money_name}\n      cents = send('\#{options[:cents_attribute]}')\n\n      if !\#{options[:allow_nil]}\n        cents ||= 0\n      end\n      \n      if cents.blank?\n        nil\n      else\n        my_currency = self.respond_to?(:currency) && !self.currency.nil? ? currency : ::Money.default_currency\n        Money.new(cents, my_currency)\n      end\n    end\n    \n    def \#{money_name}=(amount)\n      self.\#{options[:cents_attribute]} = if amount.blank?\n        nil\n      else\n        amount.to_money.cents\n      end\n    end\n  EOV\nend\n"