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.



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_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

#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

#hrefObject

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).expand(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

#resourceObject

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.

Returns:

  • (Boolean)


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