Module: JSONAPI::Consumer::Helpers::DynamicAttributes
Defined Under Namespace
Classes: DefaultKeyFormatter
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/jsonapi/consumer/helpers/dynamic_attributes.rb', line 40
def method_missing(method, *args, &block)
if has_attribute?(method)
self.class.class_eval do
define_method(method) do
attributes[method]
end
end
return send(method)
end
normalized_method = safe_key_formatter.unformat(method.to_s)
if normalized_method.end_with?('=')
set_attribute(normalized_method[0..-2], args.first)
else
super
end
end
|
Instance Method Details
#[](key) ⇒ Object
18
19
20
|
# File 'lib/jsonapi/consumer/helpers/dynamic_attributes.rb', line 18
def [](key)
read_attribute(key)
end
|
#[]=(key, value) ⇒ Object
22
23
24
|
# File 'lib/jsonapi/consumer/helpers/dynamic_attributes.rb', line 22
def []=(key, value)
set_attribute(key, value)
end
|
#attributes ⇒ Object
5
6
7
|
# File 'lib/jsonapi/consumer/helpers/dynamic_attributes.rb', line 5
def attributes
@attributes
end
|
#attributes=(attrs = {}) ⇒ Object
9
10
11
12
13
14
15
16
|
# File 'lib/jsonapi/consumer/helpers/dynamic_attributes.rb', line 9
def attributes=(attrs = {})
@attributes ||= ActiveSupport::HashWithIndifferentAccess.new
return @attributes unless attrs.present?
attrs.each do |key, value|
send("#{key}=", value)
end
end
|
#has_attribute?(attr_name) ⇒ Boolean
34
35
36
|
# File 'lib/jsonapi/consumer/helpers/dynamic_attributes.rb', line 34
def has_attribute?(attr_name)
attributes.has_key?(attr_name)
end
|
#respond_to_missing?(method, include_private = false) ⇒ Boolean
26
27
28
29
30
31
32
|
# File 'lib/jsonapi/consumer/helpers/dynamic_attributes.rb', line 26
def respond_to_missing?(method, include_private = false)
if has_attribute?(method) || method.to_s.end_with?('=')
true
else
super
end
end
|