Class: Library::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, parent: nil, nodes: []) ⇒ Node

Returns a new instance of Node.



12
13
14
15
16
# File 'lib/danta/library/node.rb', line 12

def initialize(name:, parent: nil, nodes: [])
  @name = name
  @nodes = nodes
  @parent = parent
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/danta/library/node.rb', line 7

def name
  @name
end

#nodesObject

Returns the value of attribute nodes.



8
9
10
# File 'lib/danta/library/node.rb', line 8

def nodes
  @nodes
end

#parentObject

Returns the value of attribute parent.



10
11
12
# File 'lib/danta/library/node.rb', line 10

def parent
  @parent
end

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/danta/library/node.rb', line 9

def path
  @path
end

Instance Method Details

#leaf?Boolean

Returns:

  • (Boolean)


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

def leaf?
  nodes.empty?
end

#root?Boolean

Returns:

  • (Boolean)


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

def root?
  parent.empty?
end

#to_hObject



26
27
28
29
30
31
32
# File 'lib/danta/library/node.rb', line 26

def to_h
  {
    name: name,
    path: path,
    nodes: nodes.map(&:to_h)
  }
end

#to_jsonObject



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

def to_json
  to_h.to_json
end