Class: HyperResource::Link
- Inherits:
-
Object
- Object
- HyperResource::Link
- Defined in:
- lib/hyper_resource/link.rb
Instance Attribute Summary collapse
-
#base_href ⇒ Object
Returns the value of attribute base_href.
-
#name ⇒ Object
Returns the value of attribute name.
-
#params ⇒ Object
Returns the value of attribute params.
-
#parent_resource ⇒ Object
Returns the value of attribute parent_resource.
-
#templated ⇒ Object
Returns the value of attribute templated.
Instance Method Summary collapse
- #delete(*args) ⇒ Object
-
#get(*args) ⇒ Object
Delegate HTTP methods to the resource.
-
#href ⇒ Object
Returns this link’s href, applying any URI template params.
-
#initialize(resource = nil, link_spec = {}) ⇒ Link
constructor
A new instance of Link.
-
#method_missing(method, *args, &block) ⇒ Object
If we were called with a method we don’t know, load this resource and pass the message along.
- #patch(*args) ⇒ Object
- #post(*args) ⇒ Object
- #put(*args) ⇒ Object
-
#resource ⇒ Object
Returns a HyperResource representing this link.
-
#templated? ⇒ Boolean
Returns true if this link is templated.
-
#where(params) ⇒ Object
Returns a new scope with the given params; that is, returns a copy of itself with the given params applied.
Constructor Details
#initialize(resource = nil, link_spec = {}) ⇒ Link
Returns a new instance of Link.
13 14 15 16 17 18 19 |
# File 'lib/hyper_resource/link.rb', line 13 def initialize(resource=nil, link_spec={}) self.parent_resource = resource || HyperResource.new self.base_href = link_spec['href'] self.name = link_spec['name'] self.templated = !!link_spec['templated'] self.params = link_spec['params'] || {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
If we were called with a method we don’t know, load this resource and pass the message along. This achieves implicit loading.
56 57 58 |
# File 'lib/hyper_resource/link.rb', line 56 def method_missing(method, *args, &block) self.get.send(method, *args, &block) end |
Instance Attribute Details
#base_href ⇒ Object
Returns the value of attribute base_href.
4 5 6 |
# File 'lib/hyper_resource/link.rb', line 4 def base_href @base_href end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/hyper_resource/link.rb', line 4 def name @name end |
#params ⇒ Object
Returns the value of attribute params.
4 5 6 |
# File 'lib/hyper_resource/link.rb', line 4 def params @params end |
#parent_resource ⇒ Object
Returns the value of attribute parent_resource.
4 5 6 |
# File 'lib/hyper_resource/link.rb', line 4 def parent_resource @parent_resource end |
#templated ⇒ Object
Returns the value of attribute templated.
4 5 6 |
# File 'lib/hyper_resource/link.rb', line 4 def templated @templated end |
Instance Method Details
#delete(*args) ⇒ Object
52 |
# File 'lib/hyper_resource/link.rb', line 52 def delete(*args); self.resource.delete(*args) end |
#get(*args) ⇒ Object
Delegate HTTP methods to the resource.
48 |
# File 'lib/hyper_resource/link.rb', line 48 def get(*args); self.resource.get(*args) end |
#href ⇒ Object
Returns this link’s href, applying any URI template params.
22 23 24 25 26 27 28 29 |
# File 'lib/hyper_resource/link.rb', line 22 def href if self.templated? filtered_params = self.parent_resource.outgoing_uri_filter(params) URITemplate.new(self.base_href).(filtered_params) else self.base_href end end |
#patch(*args) ⇒ Object
50 |
# File 'lib/hyper_resource/link.rb', line 50 def patch(*args); self.resource.patch(*args) end |
#post(*args) ⇒ Object
49 |
# File 'lib/hyper_resource/link.rb', line 49 def post(*args); self.resource.post(*args) end |
#put(*args) ⇒ Object
51 |
# File 'lib/hyper_resource/link.rb', line 51 def put(*args); self.resource.put(*args) end |
#resource ⇒ Object
Returns a HyperResource representing this link
43 44 45 |
# File 'lib/hyper_resource/link.rb', line 43 def resource parent_resource._hr_new_from_link(self.href) end |
#templated? ⇒ Boolean
Returns true if this link is templated.
11 |
# File 'lib/hyper_resource/link.rb', line 11 def templated?; templated end |
#where(params) ⇒ Object
Returns a new scope with the given params; that is, returns a copy of itself with the given params applied.
33 34 35 36 37 38 39 40 |
# File 'lib/hyper_resource/link.rb', line 33 def where(params) params = Hash[ params.map{|(k,v)| [k.to_s, v]} ] self.class.new(self.parent_resource, 'href' => self.base_href, 'name' => self.name, 'templated' => self.templated, 'params' => self.params.merge(params)) end |