Class: Montage::Resource
- Inherits:
-
Object
show all
- Defined in:
- lib/montage/resource.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(raw_data = {}) ⇒ Resource
Returns a new instance of Resource.
5
6
7
8
|
# File 'lib/montage/resource.rb', line 5
def initialize(raw_data = {})
@attributes = raw_data.dup.freeze if raw_data
@items = parse_items
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
43
44
45
|
# File 'lib/montage/resource.rb', line 43
def method_missing(method_name, *args, &block)
attribute_keys.include?(method_name) ? items[method_name.to_s] : super
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
3
4
5
|
# File 'lib/montage/resource.rb', line 3
def attributes
@attributes
end
|
#items ⇒ Object
Returns the value of attribute items.
3
4
5
|
# File 'lib/montage/resource.rb', line 3
def items
@items
end
|
Class Method Details
.resource_name ⇒ Object
14
15
16
|
# File 'lib/montage/resource.rb', line 14
def self.resource_name
"resource"
end
|
Instance Method Details
#attribute_keys ⇒ Object
10
11
12
|
# File 'lib/montage/resource.rb', line 10
def attribute_keys
return items.keys.map(&:to_sym) if items.is_a?(Hash)
end
|
#parse_items ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/montage/resource.rb', line 22
def parse_items
if attributes.is_a?(Hash)
{}.tap do |hsh|
attributes.each do |key, value|
if key == "_meta"
hsh["created_at"] = value["created"]
hsh["updated_at"] = value["modified"]
else
hsh[key] = value
end
end
end
else
attributes
end
end
|
#respond_to?(method_name, include_private = false) ⇒ Boolean
39
40
41
|
# File 'lib/montage/resource.rb', line 39
def respond_to?(method_name, include_private = false)
attribute_keys.include?(method_name) || super
end
|
#singular? ⇒ Boolean
18
19
20
|
# File 'lib/montage/resource.rb', line 18
def singular?
true
end
|