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(parent = PMP::CollectionDocument.new, link = {}) ⇒ Link

Returns a new instance of Link.



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

def initialize(parent=PMP::CollectionDocument.new, link={})
  super()
  self.parent = parent
  self.params = link.delete('params') || {}
  # puts "params: #{params.inspect}"
  parse_attributes(link)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pmp/link.rb', line 72

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
  if self.respond_to?(method)
    self.send(method, *args)
  elsif method.to_s.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_jsonObject



56
57
58
# File 'lib/pmp/link.rb', line 56

def as_json
  extract_attributes
end

#attributesObject



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

def attributes
  HashWithIndifferentAccess.new(marshal_dump)
end

#hrefObject



36
37
38
# File 'lib/pmp/link.rb', line 36

def href
  self[:href]
end

#href_templateObject



40
41
42
# File 'lib/pmp/link.rb', line 40

def href_template
  self[:href_template]
end

#methodObject



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

def method
  self[:method]
end

#retrieveObject



66
67
68
69
70
# File 'lib/pmp/link.rb', line 66

def retrieve
  # puts "retrieve method: #{method}"
  # puts "retrieve url: #{url}"
  parent.request((method || 'get').to_sym, url)
end

#urlObject



60
61
62
63
64
# File 'lib/pmp/link.rb', line 60

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

#where(params = {}) ⇒ Object



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

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