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
-
#get ⇒ Object
Returns a HyperResource representing this link, and fetches it.
-
#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) ⇒ Object
If we were called with a method we don't know, load this resource and pass the message along.
-
#resource ⇒ Object
Returns a HyperResource representing this link.
- #templated? ⇒ Boolean
-
#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.
12 13 14 15 16 17 18 |
# File 'lib/hyper_resource/link.rb', line 12 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) ⇒ Object
If we were called with a method we don't know, load this resource and pass the message along. This achieves implicit loading.
51 52 53 |
# File 'lib/hyper_resource/link.rb', line 51 def method_missing(method, *args) self.get.send(method, *args) 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
#get ⇒ Object
Returns a HyperResource representing this link, and fetches it.
45 46 47 |
# File 'lib/hyper_resource/link.rb', line 45 def get self.resource.get end |
#href ⇒ Object
Returns this link's href, applying any URI template params.
21 22 23 24 25 26 27 |
# File 'lib/hyper_resource/link.rb', line 21 def href if self.templated? URITemplate.new(self.base_href).(params) else self.base_href end end |
#resource ⇒ Object
Returns a HyperResource representing this link
40 41 42 |
# File 'lib/hyper_resource/link.rb', line 40 def resource parent_resource._new_from_link(self.href) end |
#templated? ⇒ Boolean
10 |
# File 'lib/hyper_resource/link.rb', line 10 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.
31 32 33 34 35 36 37 |
# File 'lib/hyper_resource/link.rb', line 31 def where(params) self.class.new(self.parent_resource, 'href' => self.base_href, 'name' => self.name, 'templated' => self.templated, 'params' => self.params.merge(params)) end |