Module: PrefixStore::ClassMethods

Defined in:
lib/prefix_store.rb

Instance Method Summary collapse

Instance Method Details

#prefix_store_accessor(store_attribute, *keys) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/prefix_store.rb', line 8

def prefix_store_accessor(store_attribute, *keys)
  keys = keys.flatten

  _store_accessors_module.module_eval do
    keys.each do |key|
      define_method("#{store_attribute}_#{key}=") do |value|
        write_store_attribute(store_attribute, key, value)
      end

      define_method("#{store_attribute}_#{key}") do
        read_store_attribute(store_attribute, key)
      end
    end
  end

  # assign new store attribute and create new hash to ensure that each class in the hierarchy
  # has its own hash of stored attributes.
  self.local_stored_attributes ||= {}
  self.local_stored_attributes[store_attribute] ||= []
  self.local_stored_attributes[store_attribute] |= keys
end