7
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
|
# File 'lib/typed-store.rb', line 7
def typed_store_accessor(store_attribute, *mappings)
keys = mappings.map(&:first).map(&:first)
store_accessor store_attribute, keys
mappings.each do |mapping|
key, type = *mapping.first
define_method "#{key}=" do |value|
value = value.utc if [DateTime, Time].include?(type) && value
write_store_attribute(store_attribute, key, value != nil ? value.to_s : nil)
end
define_method key do
value = read_store_attribute(store_attribute, key)
return nil if value == nil
return value if type == String
return value == "true" if type == Boolean
return value.to_i if type == Integer
return BigDecimal(value) if type == BigDecimal
return Time.parse(value) if type == Time
return Date.parse(value) if type == Date
return DateTime.parse(value) if type == DateTime
end
end
end
|