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



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/storext/class_methods.rb', line 22

def store_attribute(column, attr, type=nil, opts={})
  track_store_attribute(column, attr)

  storext_cast_proxy_class.attribute "_casted_#{attr}", type, opts
  unless storext_cast_proxy_class.instance_methods.include? :"_casted_#{attr}"
    raise ArgumentError, "problem defining `#{attr}`. `#{type}` may not be a valid type."
  end

  storext_define_writer(column, attr)
  storext_define_reader(column, attr)

  store_accessor column, *store_attribute_defs[column]
end

#store_attributes(column, &block) ⇒ Object



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

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

#storext_cast_proxy_classObject



45
46
47
48
49
# File 'lib/storext/class_methods.rb', line 45

def storext_cast_proxy_class
  @storext_cast_proxy_class ||= Class.new do
    include Virtus.model
  end
end

#storext_define_reader(column, attr) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/storext/class_methods.rb', line 13

def storext_define_reader(column, attr)
  define_method attr do
    if store_val = read_store_attribute(column, attr)
      storext_cast_proxy.send("_casted_#{attr}=", store_val)
    end
    storext_cast_proxy.send("_casted_#{attr}")
  end
end

#storext_define_writer(column, attr) ⇒ Object



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

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

#track_store_attribute(column, attr) ⇒ Object



40
41
42
43
# File 'lib/storext/class_methods.rb', line 40

def track_store_attribute(column, attr)
  store_attribute_defs[column] ||= []
  store_attribute_defs[column] << attr
end