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_href, #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')
  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



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pmp/link.rb', line 68

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



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

def as_json(options={})
  extract_attributes
end

#attributes_mapObject



62
63
64
65
66
# File 'lib/pmp/link.rb', line 62

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

#retrieveObject



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

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



49
50
51
52
53
# File 'lib/pmp/link.rb', line 49

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

#where(params = {}) ⇒ Object



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

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