Class: HyperResource::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/hyper_resource/link.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.



53
54
55
# File 'lib/hyper_resource/link.rb', line 53

def method_missing(method, *args)
  self.get.send(method, *args)
end

Instance Attribute Details

#base_hrefObject

Returns the value of attribute base_href.



4
5
6
# File 'lib/hyper_resource/link.rb', line 4

def base_href
  @base_href
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/hyper_resource/link.rb', line 4

def name
  @name
end

#paramsObject

Returns the value of attribute params.



4
5
6
# File 'lib/hyper_resource/link.rb', line 4

def params
  @params
end

#parent_resourceObject

Returns the value of attribute parent_resource.



4
5
6
# File 'lib/hyper_resource/link.rb', line 4

def parent_resource
  @parent_resource
end

#templatedObject

Returns the value of attribute templated.



4
5
6
# File 'lib/hyper_resource/link.rb', line 4

def templated
  @templated
end

Instance Method Details

#getObject

Returns a HyperResource representing this link, and fetches it.



47
48
49
# File 'lib/hyper_resource/link.rb', line 47

def get
  self.resource.get
end

#hrefObject

Returns this link’s href, applying any URI template params.



21
22
23
24
25
26
27
28
# File 'lib/hyper_resource/link.rb', line 21

def href
  if self.templated?
    filtered_params = self.parent_resource.outgoing_uri_filter(params)
    URITemplate.new(self.base_href).expand(filtered_params)
  else
    self.base_href
  end
end

#resourceObject

Returns a HyperResource representing this link



42
43
44
# File 'lib/hyper_resource/link.rb', line 42

def resource
  parent_resource._new_from_link(self.href)
end

#templated?Boolean

Returns:

  • (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.



32
33
34
35
36
37
38
39
# File 'lib/hyper_resource/link.rb', line 32

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