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
39
40
41
42
43
44
45
46
|
# File 'lib/datamapper-money.rb', line 13
def money(name, options = {})
raise ArgumentError.new('Name must be specified.') if name.empty?
default = options.delete(:default)
property = DataMapper::Property::Decimal.new(self, name, options)
name = property.name.to_s
instance_variable_name = property.instance_variable_name
name_cents = "#{name}_cents"
name_currency = "#{name}_currency"
options_cents = options.slice(:required, :precision, :scale).merge(:accessor => :private)
options_cents.merge!(:default => default.cents) if default
self.property name_cents.to_sym, DataMapper::Property::Decimal, options_cents
options_currency = options.slice(:required).merge(:accessor => :private, :length => 3)
options_currency.merge!(:default => default.currency.to_s) if default
self.property name_currency.to_sym, DataMapper::Property::String, options_currency
class_eval <<-RUBY, __FILE__, __LINE__ + 1
#{property.reader_visibility}
def #{name}
return #{instance_variable_name} if defined?(#{instance_variable_name})
return unless #{name_cents} && #{name_currency}
#{instance_variable_name} = Money.new(#{name_cents}, #{name_currency})
end
#{property.writer_visibility}
def #{name}=(value)
self.#{name_cents} = value.cents
self.#{name_currency} = value.currency
#{instance_variable_name} = value
end
RUBY
end
|