Module: DynamicAttributes
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/concerns/dynamic_attributes.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'app/models/concerns/dynamic_attributes.rb', line 45
def method_missing(method, *args, &block)
if self.class.define_attribute_methods && respond_to_without_attributes?(method)
send(method, *args, &block)
elsif has_dynamic_attribute?(method)
read_dynamic_attribute(method)
elsif method =~ /^([\w]+)\=$/
write_dynamic_attribute($1, args[0])
else
super
end
end
|
Instance Method Details
#[](attr_name) ⇒ Object
27
28
29
30
31
32
33
34
35
|
# File 'app/models/concerns/dynamic_attributes.rb', line 27
def [](attr_name)
if @attributes.has_key?(attr_name.to_s)
super
elsif has_dynamic_attribute?(attr_name)
read_dynamic_attribute(attr_name)
else
super
end
end
|
#[]=(key, value) ⇒ Object
37
38
39
40
41
42
43
|
# File 'app/models/concerns/dynamic_attributes.rb', line 37
def []=(key, value)
begin
super
rescue ActiveModel::MissingAttributeError
write_dynamic_attribute(key, value)
end
end
|
#attributes ⇒ Object
59
60
61
62
63
|
# File 'app/models/concerns/dynamic_attributes.rb', line 59
def attributes
attrs = super
dynamic_attrs = attrs.delete('dynamic_attributes') || {}
dynamic_attrs.merge(attrs)
end
|
#dynamic_attributes ⇒ Object
65
66
67
|
# File 'app/models/concerns/dynamic_attributes.rb', line 65
def dynamic_attributes
self['dynamic_attributes'].blank? ? {} : self['dynamic_attributes']
end
|
#has_attribute?(attr_name) ⇒ Boolean
69
70
71
|
# File 'app/models/concerns/dynamic_attributes.rb', line 69
def has_attribute?(attr_name)
super || has_dynamic_attribute?(attr_name)
end
|