Class: Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_hash, id, parent_id) ⇒ Node

Returns a new instance of Node.



7
8
9
10
11
12
13
# File 'lib/pomona/node.rb', line 7

def initialize(data_hash, id, parent_id)
  @id = id
  node_data = { id: @id, children: [] }
  @node = data_hash.merge(node_data)
  @children = @node[:children]
  @parent_id = parent_id
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



5
6
7
# File 'lib/pomona/node.rb', line 5

def children
  @children
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#nodeObject

Returns the value of attribute node.



5
6
7
# File 'lib/pomona/node.rb', line 5

def node
  @node
end

#parent_idObject (readonly)

Returns the value of attribute parent_id.



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

def parent_id
  @parent_id
end

Instance Method Details

#has_children?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/pomona/node.rb', line 15

def has_children?
  children.any?
end

#has_grandchildren?Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
# File 'lib/pomona/node.rb', line 19

def has_grandchildren?
  if has_children?
    children.select { |child| child.has_children? }.any?
  else
    false
  end
end