Class: PMP::Links

Inherits:
HashWithIndifferentAccess
  • Object
show all
Includes:
Utils
Defined in:
lib/pmp/links.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#to_json_key_name, #to_ruby_safe_name

Constructor Details

#initialize(parent) ⇒ Links

Returns a new instance of Links.



10
11
12
13
# File 'lib/pmp/links.rb', line 10

def initialize(parent)
  super()
  self.parent = parent
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



8
9
10
# File 'lib/pmp/links.rb', line 8

def parent
  @parent
end

Instance Method Details

#[]=(k, link) ⇒ Object



15
16
17
18
19
# File 'lib/pmp/links.rb', line 15

def []=(k, link)
  super
  set_parent(link)
  add_link_name(k)
end


21
22
23
24
25
26
27
# File 'lib/pmp/links.rb', line 21

def add_link_name(name)
  name = name.to_sym
  unless parent.respond_to?(name)
    parent.define_singleton_method(name) { self.links[name] }
    parent.define_singleton_method("#{name}=") { |x| self.links[name] = x }
  end
end

#set_parent(link) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/pmp/links.rb', line 29

def set_parent(link)
  if link.respond_to?(:parent)
    link.parent = self.parent
  elsif link.is_a?(Hash)
    link.values.each{|l| set_parent(l)}
  elsif link.is_a?(Enumerable)
    link.each{|l| set_parent(l)}
  end
end