Class: TTK::Logger::SectionNode

Inherits:
Object
  • Object
show all
Defined in:
lib/ttk/logger/section_node.rb

Overview

Implement a tree of section. A section node has a name and a ‘active’ flag that say if the section node is on/off. A section tree verifies the following property:

a node is active iff at least one of its sub nodes is active

Obviously, when activating a node, all its sub nodes are also activated.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *sub_sections) ⇒ SectionNode

Returns a new instance of SectionNode.



20
21
22
23
24
25
# File 'lib/ttk/logger/section_node.rb', line 20

def initialize(name, *sub_sections)
  self.name = name
  @sub_sections = {}
  sub_sections.each { |s| self << s }
  @active = false
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



27
28
29
# File 'lib/ttk/logger/section_node.rb', line 27

def name
  @name
end

Instance Method Details

#<<(sub_section) ⇒ Object



47
48
49
50
# File 'lib/ttk/logger/section_node.rb', line 47

def <<(sub_section)
  check_sub_section_type(sub_section)
  @sub_sections[sub_section.name] = sub_section
end

#[](name) ⇒ Object



37
38
39
# File 'lib/ttk/logger/section_node.rb', line 37

def [](name)
  @sub_sections[name]
end

#[]=(name, sub_section) ⇒ Object



41
42
43
44
45
# File 'lib/ttk/logger/section_node.rb', line 41

def []=(name, sub_section)
  check_sub_section_type(sub_section)
  sub_section.name = name
  @sub_sections[sub_section.name] = sub_section
end

#active=(new_active) ⇒ Object



56
57
58
59
# File 'lib/ttk/logger/section_node.rb', line 56

def active=(new_active)
  active_tree(new_active)
  @active
end

#active?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/ttk/logger/section_node.rb', line 61

def active?
  @active
end

#active_section(section_name) ⇒ Object

Active the given section name in the tree. Return an array of all the section name that have been activated. The returned array is empty if the section name doesn’t exists in the tree.



81
82
83
84
85
# File 'lib/ttk/logger/section_node.rb', line 81

def active_section(section_name)
  accu = []
  active_section_rec(section_name, accu)
  accu
end

#active_tree(new_active) ⇒ Object



65
66
67
68
69
# File 'lib/ttk/logger/section_node.rb', line 65

def active_tree(new_active)
  accu = []
  active_tree_rec(new_active, accu) unless new_active == @active
  accu
end

#clear(name) ⇒ Object



168
169
170
# File 'lib/ttk/logger/section_node.rb', line 168

def clear(name)
  @sub_sections.clear
end

#delete(name) ⇒ Object



164
165
166
# File 'lib/ttk/logger/section_node.rb', line 164

def delete(name)
  @sub_sections.delete(name)
end

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



146
147
148
# File 'lib/ttk/logger/section_node.rb', line 146

def each(&block)
  @sub_sections.each(&block)
end

#each_label(&block) ⇒ Object Also known as: each_key



158
159
160
# File 'lib/ttk/logger/section_node.rb', line 158

def each_label(&block)
  @sub_sections.each_key(&block)
end

#each_section(&block) ⇒ Object Also known as: each_value



152
153
154
# File 'lib/ttk/logger/section_node.rb', line 152

def each_section(&block)
  @sub_sections.each_value(&block)
end

#find(name) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
# File 'lib/ttk/logger/section_node.rb', line 189

def find(name)
  if @name == name
    self
  else
    @sub_sections.each_value do |s|
      ret = s.find(name)
      return ret unless ret.nil?
    end
    nil
  end
end

#leaf?Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/ttk/logger/section_node.rb', line 179

def leaf?
  @sub_sections.empty?
end

#nb_sub_sectionsObject Also known as: size, length



172
173
174
# File 'lib/ttk/logger/section_node.rb', line 172

def nb_sub_sections
  @sub_sections.size
end

#pre_depth_first(&block) ⇒ Object



183
184
185
186
187
# File 'lib/ttk/logger/section_node.rb', line 183

def pre_depth_first(&block)
  block[self]
  @sub_sections.each_value { |s| s.pre_depth_first(&block) }
  nil
end

#push(*sub_sections) ⇒ Object



52
53
54
# File 'lib/ttk/logger/section_node.rb', line 52

def push(*sub_sections)
  sub_sections.each { |ss| self << ss }
end

#set_active_section(active, *section_names) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/ttk/logger/section_node.rb', line 137

def set_active_section(active, *section_names)
  section = []
  section_names.each do |s|
    section.concat(active ? active_section(s) : unactive_section(s))
  end
  section.uniq!
  section
end

#sub_sectionsObject



33
34
35
# File 'lib/ttk/logger/section_node.rb', line 33

def sub_sections
  @sub_sections.values
end

#unactive_section(section_name) ⇒ Object

Unactive the given section name in the tree. Return an array of all the section name that have been unactivated. The returned array is empty if the section name doesn’t exists in the tree or if none section have been unactivated.



110
111
112
113
114
# File 'lib/ttk/logger/section_node.rb', line 110

def unactive_section(section_name)
  accu = []
  unactive_section_rec(section_name, accu)
  accu
end