Module: TypedStore::ClassMethods

Defined in:
lib/typed-store.rb

Instance Method Summary collapse

Instance Method Details

#typed_store_accessor(store_attribute, *mappings) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/typed-store.rb', line 5

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 = read_store_attribute(store_attribute, key)
      return nil unless value
      return value if type == String
      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