Module: ActsAsDynamicMethod::InstanceMethods
- Defined in:
- lib/acts_as_dynamic_method.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/acts_as_dynamic_method.rb', line 39
def method_missing(meth, *args, &block)
method_name = meth.to_s
if json_keys.include?(method_name)
self.use(method_name)
elsif json_keys.include?(method_name.gsub!('=', ''))
self.set(method_name, args.first)
elsif method_name.match(/\?$/)
self.json_keys.include?(method_name.gsub('?', ''))
else
super
end
end
|
Instance Method Details
#fields ⇒ Object
16
17
18
19
20
|
# File 'lib/acts_as_dynamic_method.rb', line 16
def fields
field_name = self.class.json_field
field = self.method(field_name)
@fields ||= ((field.call.is_a?(String) ? JSON.parse(field.call) : field.call) || {})
end
|
#json_keys ⇒ Object
31
32
33
|
# File 'lib/acts_as_dynamic_method.rb', line 31
def json_keys
fields.keys
end
|
#set(field, value) ⇒ Object
26
27
28
29
|
# File 'lib/acts_as_dynamic_method.rb', line 26
def set(field, value)
fields unless @fields
@fields[field] = value
end
|
#to_json ⇒ Object
35
36
37
|
# File 'lib/acts_as_dynamic_method.rb', line 35
def to_json
fields.to_json
end
|
#use(field) ⇒ Object
22
23
24
|
# File 'lib/acts_as_dynamic_method.rb', line 22
def use(field)
fields[field]
end
|