Class: VRT::Node
- Inherits:
-
Object
- Object
- VRT::Node
- Defined in:
- lib/vrt/node.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#priority ⇒ Object
readonly
Returns the value of attribute priority.
-
#qualified_vrt_id ⇒ Object
readonly
Returns the value of attribute qualified_vrt_id.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
-
#as_json(options = nil) ⇒ Object
Since this object contains references to parent and children, as_json must be overridden to avoid unending recursion.
- #children? ⇒ Boolean
- #construct_vrt_id ⇒ Object
- #id_list ⇒ Object
-
#initialize(attributes = {}) ⇒ Node
constructor
A new instance of Node.
- #mappings ⇒ Object
- #third_party_links ⇒ Object
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
#children ⇒ Object
Returns the value of attribute children.
4 5 6 |
# File 'lib/vrt/node.rb', line 4 def children @children end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
3 4 5 |
# File 'lib/vrt/node.rb', line 3 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/vrt/node.rb', line 3 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
3 4 5 |
# File 'lib/vrt/node.rb', line 3 def parent @parent end |
#priority ⇒ Object (readonly)
Returns the value of attribute priority.
3 4 5 |
# File 'lib/vrt/node.rb', line 3 def priority @priority end |
#qualified_vrt_id ⇒ Object (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 |
#type ⇒ Object (readonly)
Returns the value of attribute type.
3 4 5 |
# File 'lib/vrt/node.rb', line 3 def type @type end |
#version ⇒ Object (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( = 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() end else instance_variable_get(attribute) end end json end |
#children? ⇒ Boolean
18 19 20 |
# File 'lib/vrt/node.rb', line 18 def children? @has_children end |
#construct_vrt_id ⇒ Object
22 23 24 |
# File 'lib/vrt/node.rb', line 22 def construct_vrt_id id_list.join('.') end |
#id_list ⇒ Object
34 35 36 |
# File 'lib/vrt/node.rb', line 34 def id_list parent ? parent.id_list << id : [id] end |
#mappings ⇒ Object
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 |
#third_party_links ⇒ Object
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 |