Class: PMP::Link

Inherits:
OpenStruct
  • Object
show all
Includes:
Parser
Defined in:
lib/pmp/link.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Parser

#extract_attributes, #extract_links, #parse, #parse_attributes, #parse_items, #parse_link, #parse_links, #parse_links_list, #parse_version

Methods included from Utils

#to_json_key_name, #to_ruby_safe_name

Constructor Details

#initialize(link = {}, parent = nil) ⇒ Link

Returns a new instance of Link.



28
29
30
31
32
33
34
35
# File 'lib/pmp/link.rb', line 28

def initialize(link={}, parent=nil)
  super()
  self.parent = parent || link.delete('parent') || PMP::CollectionDocument.new
  self.params = link.delete('params') || {}
  # puts "params: #{params.inspect}"
  parse_attributes(link)
  [:href, :href_template, :method].each{|m| self.send("#{m}=", nil) unless respond_to?(m)}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pmp/link.rb', line 64

def method_missing(method, *args)
  # puts "mm: #{method}"
  # this is a method the link supports, call the link
  # if this is an assignment, assign to the link
  # if you want to assign to a linked doc(s), need to retrieve first
  method_last = method.to_s.last
  if method_last == '='
   super
  else
    # puts "mm retrieve and send: #{method}"
    self.retrieve.send(method, *args)
  end
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



26
27
28
# File 'lib/pmp/link.rb', line 26

def params
  @params
end

#parentObject

Returns the value of attribute parent.



24
25
26
# File 'lib/pmp/link.rb', line 24

def parent
  @parent
end

Instance Method Details

#as_json(options = {}) ⇒ Object



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

def as_json(options={})
  extract_attributes
end

#attributesObject



37
38
39
40
41
# File 'lib/pmp/link.rb', line 37

def attributes
  attrs = HashWithIndifferentAccess.new(marshal_dump)
  attrs.delete(attrs[:href_template].blank? ? :href_template : :href)
  attrs
end

#retrieveObject



57
58
59
60
61
62
# File 'lib/pmp/link.rb', line 57

def retrieve
  # puts "retrieve method: #{method}"
  # puts "retrieve url: #{url}"
  # response = parent.request((method || 'get').to_sym, url)
  @doc ||= PMP::CollectionDocument.new(parent.options.merge(href: url, root: parent.root))
end

#urlObject



51
52
53
54
55
# File 'lib/pmp/link.rb', line 51

def url
  # puts "url href_template: #{href_template}"
  # puts "url href: #{href}"
  URITemplate.new(href_template || href).expand(params)
end

#where(params = {}) ⇒ Object



43
44
45
# File 'lib/pmp/link.rb', line 43

def where(params={})
  self.class.new(attributes.merge({'params'=>params}), parent)
end