Module: Storext::ClassMethods

Defined in:
lib/storext/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#store_attribute(column, attr, type = nil, opts = {}) ⇒ Object



25
26
27
28
29
30
# File 'lib/storext/class_methods.rb', line 25

def store_attribute(column, attr, type=nil, opts={})
  track_store_attribute(column, attr, type, opts)
  storext_check_attr_validity(attr, type, opts)
  storext_define_accessor(column, attr)
  store_accessor column, *storext_attrs_for(column)
end

#store_attributes(column, &block) ⇒ Object



37
38
39
# File 'lib/storext/class_methods.rb', line 37

def store_attributes(column, &block)
  AttributeProxy.new(self, column, &block).define_store_attribute
end

#storext_define_accessor(column, attr) ⇒ Object



32
33
34
35
# File 'lib/storext/class_methods.rb', line 32

def storext_define_accessor(column, attr)
  storext_define_writer(column, attr)
  storext_define_reader(column, attr)
end

#storext_define_reader(column, attr) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/storext/class_methods.rb', line 15

def storext_define_reader(column, attr)
  define_method attr do
    if send(column) && send(column).has_key?(attr.to_s)
      store_val = read_store_attribute(column, attr)
      storext_cast_proxy(attr).send("casted_attr=", store_val)
    end
    storext_cast_proxy(attr).send("casted_attr")
  end
end

#storext_define_writer(column, attr) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/storext/class_methods.rb', line 4

def storext_define_writer(column, attr)
  define_method "#{attr}=" do |value|
    storext_cast_proxy(attr).send("casted_attr=", value)
    send("#{column}=", send(column) || {})

    attr_value = storext_cast_proxy(attr).send("casted_attr")
    write_store_attribute column, attr, value
    send(column)[attr.to_s] = value
  end
end