Class: VRT::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/vrt/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Node

Returns a new instance of Node.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/vrt/node.rb', line 6

def initialize(attributes = {})
  @id = attributes['id'].to_sym
  @name = attributes['name']
  @priority = attributes['priority']
  @type = attributes['type']
  @has_children = attributes.key?('children')
  @children = {}
  @version = attributes['version']
  @parent = attributes['parent']
  @qualified_vrt_id = construct_vrt_id
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



4
5
6
# File 'lib/vrt/node.rb', line 4

def children
  @children
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/vrt/node.rb', line 3

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/vrt/node.rb', line 3

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



3
4
5
# File 'lib/vrt/node.rb', line 3

def parent
  @parent
end

#priorityObject (readonly)

Returns the value of attribute priority.



3
4
5
# File 'lib/vrt/node.rb', line 3

def priority
  @priority
end

#qualified_vrt_idObject (readonly)

Returns the value of attribute qualified_vrt_id.



3
4
5
# File 'lib/vrt/node.rb', line 3

def qualified_vrt_id
  @qualified_vrt_id
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/vrt/node.rb', line 3

def type
  @type
end

#versionObject (readonly)

Returns the value of attribute version.



3
4
5
# File 'lib/vrt/node.rb', line 3

def version
  @version
end

Instance Method Details

#as_json(options = nil) ⇒ Object

Since this object contains references to parent and children, as_json must be overridden to avoid unending recursion.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/vrt/node.rb', line 40

def as_json(options = nil)
  json = {}
  instance_variables.each do |attribute|
    attr_name = attribute.to_s.tr('@', '')
    json[attr_name] = case attr_name
                      when 'parent'
                        parent&.qualified_vrt_id
                      when 'children'
                        children.inject({}) do |c, (k, v)|
                          c[k] = v.nil? ? v : v.as_json(options)
                        end
                      else
                        instance_variable_get(attribute)
                      end
  end
  json
end

#children?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/vrt/node.rb', line 18

def children?
  @has_children
end

#construct_vrt_idObject



22
23
24
# File 'lib/vrt/node.rb', line 22

def construct_vrt_id
  id_list.join('.')
end

#id_listObject



34
35
36
# File 'lib/vrt/node.rb', line 34

def id_list
  parent ? parent.id_list << id : [id]
end

#mappingsObject



26
27
28
# File 'lib/vrt/node.rb', line 26

def mappings
  Hash[VRT.mappings.map { |name, map| [name, map.get(id_list, @version)] }]
end


30
31
32
# File 'lib/vrt/node.rb', line 30

def third_party_links
  Hash[VRT.third_party_links.map { |name, map| [name, map.get(id_list, @version)] }]
end