| Class | Uttk::Logger::SectionNode |
| In: |
lib/uttk/logger/section_node.rb
|
| Parent: | Object |
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.
| name | [R] |
# File lib/uttk/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
# File lib/uttk/logger/section_node.rb, line 47 def <<(sub_section) check_sub_section_type(sub_section) @sub_sections[sub_section.name] = sub_section end
# File lib/uttk/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
# File lib/uttk/logger/section_node.rb, line 56 def active=(new_active) active_tree(new_active) @active end
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.
# File lib/uttk/logger/section_node.rb, line 81 def active_section(section_name) accu = [] active_section_rec(section_name, accu) accu end
# File lib/uttk/logger/section_node.rb, line 65 def active_tree(new_active) accu = [] active_tree_rec(new_active, accu) unless new_active == @active accu end
# File lib/uttk/logger/section_node.rb, line 158 def each_label(&block) @sub_sections.each_key(&block) end
# File lib/uttk/logger/section_node.rb, line 152 def each_section(&block) @sub_sections.each_value(&block) end
# File lib/uttk/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
# File lib/uttk/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
# File lib/uttk/logger/section_node.rb, line 52 def push(*sub_sections) sub_sections.each { |ss| self << ss } end
# File lib/uttk/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
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.
# File lib/uttk/logger/section_node.rb, line 110 def unactive_section(section_name) accu = [] unactive_section_rec(section_name, accu) accu end
# File lib/uttk/logger/section_node.rb, line 87 def active_section_rec(section_name, accu) if @name == section_name accu.concat(active_tree(true)) true else ret = false @sub_sections.each_value do |s| if s.active_section_rec(section_name, accu) @active = true accu << @name ret = true break end end ret end end
# File lib/uttk/logger/section_node.rb, line 71 def active_tree_rec(new_active, accu) @active = new_active accu << @name @sub_sections.each_value { |s| s.active_tree_rec(new_active, accu) } end
# File lib/uttk/logger/section_node.rb, line 202 def check_sub_section_type(sub_section) unless sub_section.is_a?(self.class) raise(TypeError, "`#{sub_section}' - must be a #{self.class}") end end
# File lib/uttk/logger/section_node.rb, line 116 def unactive_section_rec(section_name, accu) if @name == section_name accu.concat(active_tree(false)) true else not_unactivable = ret = false @sub_sections.each_value do |s| ret ||= s.unactive_section_rec(section_name, accu) not_unactivable ||= s.active? end if ret and (not not_unactivable) @active = false accu << @name true else false end end end