Module: Datmachine::Resource
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/datmachine/resources/resource.rb', line 145
def method_missing(method, *args, &block)
if @attributes.has_key?(method.to_s)
return @attributes[method.to_s]
end
case method.to_s
when /(.+)=$/
attr = method.to_s.chop
@attributes[attr] = args[0]
else
if @hyperlinks.has_key? "#{method}"
value = @hyperlinks["#{method}"]
result = value.call
return result
else
super
end
end
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
11
12
13
|
# File 'lib/datmachine/resources/resource.rb', line 11
def attributes
@attributes
end
|
#href ⇒ Object
Returns the value of attribute href.
15
16
17
|
# File 'lib/datmachine/resources/resource.rb', line 15
def href
@href
end
|
#hyperlinks ⇒ Object
Returns the value of attribute hyperlinks.
12
13
14
|
# File 'lib/datmachine/resources/resource.rb', line 12
def hyperlinks
@hyperlinks
end
|
#id ⇒ Object
Returns the value of attribute id.
14
15
16
|
# File 'lib/datmachine/resources/resource.rb', line 14
def id
@id
end
|
#links ⇒ Object
Returns the value of attribute links.
16
17
18
|
# File 'lib/datmachine/resources/resource.rb', line 16
def links
@links
end
|
Class Method Details
.included(base) ⇒ Object
191
192
193
|
# File 'lib/datmachine/resources/resource.rb', line 191
def self.included(base)
base.extend ClassMethods
end
|
Instance Method Details
#copy_from(other) ⇒ Object
139
140
141
142
143
|
# File 'lib/datmachine/resources/resource.rb', line 139
def copy_from(other)
other.instance_variables.each do |ivar|
instance_variable_set ivar, other.instance_variable_get(ivar)
end
end
|
#does_resource_respond_to?(method_name) ⇒ Boolean
187
188
189
|
# File 'lib/datmachine/resources/resource.rb', line 187
def does_resource_respond_to?(method_name)
@attributes.has_key?(method_name.to_s) or @hyperlinks.has_key?(method_name.to_s)
end
|
#fetch(*arguments) ⇒ Object
Also known as:
find
delegate the query to the pager module
85
86
87
|
# File 'lib/datmachine/resources/resource.rb', line 85
def fetch(*arguments)
self.class.find *arguments
end
|
#hydrate(links, meta) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/datmachine/resources/resource.rb', line 43
def hydrate(links, meta)
links.each do |key, uri|
property = key.sub(/.*?\./, '')
if self.links.include? property
link_value = self.links[property]
template = Addressable::Template.new(uri)
uri = template.expand("#{key}" => link_value).to_s
@hyperlinks[property] = Datmachine::Utils.callable(
link_value.nil? ? link_value : lambda { self.class.find(uri) }
)
else
unless uri.nil? || uri.is_a?(Hash)
begin
match_data = /\{(#{self.class.collection_name}\.(\w+))\}/.match(uri)
rescue TypeError => ex
puts 'what the fuck'
raise ex
end
unless match_data.nil?
attribute_path = match_data[1]
attribute_name = match_data[2]
template = Addressable::Template.new(uri)
uri = template.expand("#{attribute_path}" => @attributes[attribute_name]).to_s
end
end
@hyperlinks[property] = Datmachine::Utils.callable(Datmachine::Pager.new(uri, {}))
end
end
end
|
#initialize(attributes = {}) ⇒ Object
18
19
20
21
|
# File 'lib/datmachine/resources/resource.rb', line 18
def initialize(attributes = {})
@attributes = Utils.indifferent_read_access attributes
@hyperlinks = {}
end
|
#reload(the_response = nil) ⇒ Object
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/datmachine/resources/resource.rb', line 128
def reload(the_response = nil)
if the_response
return if the_response.body.to_s.length.zero?
fresh = self.class.construct_from_response the_response.body
else
fresh = self.find(@attributes[:href])
end
fresh and copy_from fresh
return fresh
end
|
#respond_to?(method_name, include_private = false) ⇒ Boolean
178
179
180
|
# File 'lib/datmachine/resources/resource.rb', line 178
def respond_to?(method_name, include_private=false)
does_resource_respond_to?(method_name) || super
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
182
183
184
|
# File 'lib/datmachine/resources/resource.rb', line 182
def respond_to_missing?(method_name, include_private=false)
does_resource_respond_to?(method_name) || super
end
|
#sanitize ⇒ Object
107
108
109
110
111
112
113
114
115
|
# File 'lib/datmachine/resources/resource.rb', line 107
def sanitize
to_submit = {}
@attributes.each do |key, value|
unless value.is_a? Datmachine::Resource
to_submit[key] = value
end
end
to_submit
end
|
#save ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/datmachine/resources/resource.rb', line 91
def save
href = @attributes.delete('href')
method = :post
if href.nil?
href = self.class.collection_path
elsif !Datmachine.is_collection(href)
method = :put
end
attributes_to_submit = self.sanitize
@response = Datmachine.send(method, href, attributes_to_submit)
@attributes['href'] = href
reload @response
end
|
#unstore ⇒ Object
Also known as:
destroy
123
124
125
|
# File 'lib/datmachine/resources/resource.rb', line 123
def unstore
Datmachine.unstore @attributes[:href]
end
|