Module: CustomAttributes::HasCustomAttributes::InstanceMethods
- Defined in:
- lib/custom_attributes/has_custom_attributes.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &blk) ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/custom_attributes/has_custom_attributes.rb', line 89
def method_missing(meth, *args, &blk)
name = meth.to_s.gsub(/\=$/, '')
if custom_attribute_names.include?(name)
if meth =~ /\=$/
self[name] = args.first
else
self[name] || read_custom_attribute(name)
end
else
super
end
end
|
Instance Method Details
#[](name) ⇒ Object
49
50
51
|
# File 'lib/custom_attributes/has_custom_attributes.rb', line 49
def [](name)
custom_attributes[name] || super
end
|
#[]=(name, value) ⇒ Object
53
54
55
56
57
58
59
|
# File 'lib/custom_attributes/has_custom_attributes.rb', line 53
def []=(name, value)
if custom_attribute_names.include?(name)
custom_attributes[name] = value
else
super
end
end
|
#attributes ⇒ Object
69
70
71
|
# File 'lib/custom_attributes/has_custom_attributes.rb', line 69
def attributes
super.merge custom_attributes
end
|
#attributes=(hash) ⇒ Object
61
62
63
64
65
66
67
|
# File 'lib/custom_attributes/has_custom_attributes.rb', line 61
def attributes=(hash)
custom_attribute_names.each do |name|
if hash.keys.include?(name)
custom_attributes[name] = hash.delete(name)
end
end
end
|
#custom_attribute_names ⇒ Object
85
86
87
|
# File 'lib/custom_attributes/has_custom_attributes.rb', line 85
def custom_attribute_names
self.class.custom_attributes.map(&:name)
end
|
#custom_attributes ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/custom_attributes/has_custom_attributes.rb', line 73
def custom_attributes
@custom_attributes ||= {}
@custom_attributes[::CustomAttributes.scope || :no_scope] ||= begin
all_attributes = self.class.custom_attributes
all_attributes.inject({}) do |attribs, custom_attrib|
attribs[custom_attrib.name] = read_custom_attribute(custom_attrib.name)
attribs
end
end
end
|