Class: HOALife::Resource
- Inherits:
-
Object
show all
- Extended by:
- Forwardable, Arrayable
- Defined in:
- lib/hoalife/resource.rb
Overview
Constant Summary
Constants included
from Arrayable
Arrayable::BLANK_RE
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Arrayable
as_array
Constructor Details
#initialize(attrs = {}, _relationships = {}) ⇒ Resource
Returns a new instance of Resource.
43
44
45
46
47
48
49
|
# File 'lib/hoalife/resource.rb', line 43
def initialize(attrs = {}, _relationships = {})
@attrs = {}
cast_attrs(attrs.transform_keys(&:to_sym)).each do |k, v|
send("#{k}=".to_sym, v)
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &blk) ⇒ Object
70
71
72
73
74
75
76
77
78
|
# File 'lib/hoalife/resource.rb', line 70
def method_missing(method_name, *args, &blk)
if method_name.match(/\=$/)
@attrs[method_name.to_s.gsub(/\=$/, "").to_sym] = args.first
elsif @attrs.key?(method_name)
@attrs[method_name]
else
super
end
end
|
Class Attribute Details
.base_path ⇒ Object
Returns the value of attribute base_path.
17
18
19
|
# File 'lib/hoalife/resource.rb', line 17
def base_path
@base_path
end
|
Instance Attribute Details
#attrs ⇒ Object
Returns the value of attribute attrs.
12
13
14
|
# File 'lib/hoalife/resource.rb', line 12
def attrs
@attrs
end
|
Class Method Details
.new(obj = {}, relationships = {}) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/hoalife/resource.rb', line 21
def new(obj = {}, relationships = {})
return super(obj, relationships) unless obj['type']
camelized = HOALInflector.new.camelize(obj['type'], nil)
begin
klass = Object.const_get("HOALife::#{camelized}")
rescue NameError
raise HOALife::UndefinedResourceError,
"HOALife::#{camelized} is not defined"
end
klass.new(obj['attributes'], obj['relationships'])
end
|
.resource_collection ⇒ Object
36
37
38
39
40
|
# File 'lib/hoalife/resource.rb', line 36
def resource_collection
@resource_collection ||= HOALife::Resources::Collection.new(
HOALife.api_base + base_path
)
end
|
Instance Method Details
#==(other) ⇒ Object
86
87
88
|
# File 'lib/hoalife/resource.rb', line 86
def ==(other)
@attrs == other.attrs
end
|
#as_json ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/hoalife/resource.rb', line 51
def as_json
h = {
'data' => {
'attributes' => {},
'relationships' => {}
}
}
@attrs.each do |k, _v|
h['data']['attributes'][k] = send(k)
end
h
end
|
#respond_to?(method_name, include_private = false) ⇒ Boolean
80
81
82
83
84
|
# File 'lib/hoalife/resource.rb', line 80
def respond_to?(method_name, include_private = false)
!@attrs[method_name] ||
method_name.match(/\=$/) ||
super
end
|
#to_json(*_args) ⇒ Object
66
67
68
|
# File 'lib/hoalife/resource.rb', line 66
def to_json(*_args)
JSON.generate as_json
end
|