Class: RubyRich::Tree::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_rich/tree.rb

Overview

树节点类

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, data: nil) ⇒ Node

Returns a new instance of Node.



39
40
41
42
43
44
# File 'lib/ruby_rich/tree.rb', line 39

def initialize(name, data: nil)
  @name = name
  @children = []
  @data = data
  @expanded = true
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



37
38
39
# File 'lib/ruby_rich/tree.rb', line 37

def children
  @children
end

#dataObject

Returns the value of attribute data.



37
38
39
# File 'lib/ruby_rich/tree.rb', line 37

def data
  @data
end

#expandedObject

Returns the value of attribute expanded.



37
38
39
# File 'lib/ruby_rich/tree.rb', line 37

def expanded
  @expanded
end

#nameObject

Returns the value of attribute name.



37
38
39
# File 'lib/ruby_rich/tree.rb', line 37

def name
  @name
end

Instance Method Details

#add(name, data: nil) ⇒ Object



51
52
53
54
55
# File 'lib/ruby_rich/tree.rb', line 51

def add(name, data: nil)
  child = Node.new(name, data: data)
  add_child(child)
  child
end

#add_child(child_node) ⇒ Object



46
47
48
49
# File 'lib/ruby_rich/tree.rb', line 46

def add_child(child_node)
  @children << child_node
  child_node
end

#has_children?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/ruby_rich/tree.rb', line 61

def has_children?
  !@children.empty?
end

#leaf?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/ruby_rich/tree.rb', line 57

def leaf?
  @children.empty?
end