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



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

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, attr)
end

#store_attributes(column, &block) ⇒ Object



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

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

#storext_define_accessor(column, attr) ⇒ Object



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

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

#storext_define_predicater(column, attr) ⇒ Object



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

def storext_define_predicater(column, attr)
  define_method "#{attr}?" do
    return false unless send(column) && send(column).has_key?(attr.to_s)
    !!read_store_attribute(column, attr)
  end
end

#storext_define_proxy_attribute(attr) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/storext/class_methods.rb', line 49

def storext_define_proxy_attribute(attr)
  compute_default_method_name = :"compute_default_#{attr}"
  definition = self.storext_definitions[attr]
  proxy_class = self.storext_proxy_class
  proxy_class.attribute(
    attr,
    definition[:type],
    definition[:opts].merge(default: compute_default_method_name),
  )

  proxy_class.send :define_method, compute_default_method_name do
    default_value = definition[:opts][:default]
    if default_value.is_a?(Symbol)
      source.send(default_value)
    elsif default_value.respond_to?(:call)
      attribute = self.class.attribute_set[attr]
      default_value.call(source, attribute)
    else
      default_value
    end
  end
end

#storext_define_reader(column, attr) ⇒ Object



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

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.send("#{attr}=", store_val)
    end
    storext_cast_proxy.send("#{attr}")
  end
end

#storext_define_writer(column, attr) ⇒ Object



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

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

    send("#{column}=", send(column) || {})
    write_store_attribute column, attr, coerced_value
    send(column)[attr.to_s] = coerced_value
  end
end

#storext_proxy_classObject



72
73
74
75
76
77
# File 'lib/storext/class_methods.rb', line 72

def storext_proxy_class
  Storext.proxy_classes[self] ||= Class.new(*Storext.proxy_classes[self.superclass]) do
    include Virtus.model
    attribute :source
  end
end