Module: MenuTxt::Node

Includes:
Enumerable
Included in:
SimpleNode
Defined in:
lib/menu_txt/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



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

def children
  @children
end

#levelObject

Returns the value of attribute level.



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

def level
  @level
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#<<(node) ⇒ Object



30
31
32
33
34
# File 'lib/menu_txt/node.rb', line 30

def <<(node)
  node.parent = self
  node.level = level + 1
  children << node
end

#add_child(name, url) ⇒ Object



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

def add_child(name, url)
  self << self.class.new(name, url)
end

#initialize(name, url = '') ⇒ Object



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

def initialize(name, url = '')
  @name, @url, @parent, @children, @level = name, url, nil, [], -1
end

#leaf?Boolean

Returns:

  • (Boolean)


11
# File 'lib/menu_txt/node.rb', line 11

def leaf?; children.empty? end

#traverse(&block) ⇒ Object Also known as: each



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/menu_txt/node.rb', line 13

def traverse(&block)
  if block_given?
    yield(self) if parent

    children.each do |child|
      child.traverse(&block)
    end
  else
    enum_for
  end
end