Module: ActiveRecord::Store::ClassMethods

Defined in:
activerecord/lib/active_record/store.rb

Instance Method Summary collapse

Instance Method Details

#_store_accessors_moduleObject


93
94
95
96
97
98
99
# File 'activerecord/lib/active_record/store.rb', line 93

def _store_accessors_module
  @_store_accessors_module ||= begin
    mod = Module.new
    include mod
    mod
  end
end

#store(store_attribute, options = {}) ⇒ Object


69
70
71
72
# File 'activerecord/lib/active_record/store.rb', line 69

def store(store_attribute, options = {})
  serialize store_attribute, IndifferentCoder.new(options[:coder])
  store_accessor(store_attribute, options[:accessors]) if options.has_key? :accessors
end

#store_accessor(store_attribute, *keys) ⇒ Object


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'activerecord/lib/active_record/store.rb', line 74

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

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

      define_method(key) do
        read_store_attribute(store_attribute, key)
      end
    end
  end

  self.stored_attributes[store_attribute] ||= []
  self.stored_attributes[store_attribute] |= keys
end