Class: ConstConf::Tree
- Inherits:
-
Object
- Object
- ConstConf::Tree
- Extended by:
- Term::ANSIColor
- Defined in:
- lib/const_conf/tree.rb
Overview
A tree structure implementation for visualizing ConstConf configuration hierarchies.
The Tree class provides a hierarchical representation of ConstConf modules and settings, allowing for formatted display of configuration data with metadata including prefixes, environment variable names, default values, and configuration status. It supports colored output and proper indentation to show the relationships between nested configuration elements.
Class Method Summary collapse
-
.from_const_conf(configuration_or_setting) ⇒ ConstConf::Tree
Converts a ConstConf configuration or setting into a tree structure for display purposes.
Instance Method Summary collapse
-
#<<(child) ⇒ Array<ConstConf::Tree>
Adds a child node to this tree node’s collection of children.
-
#default_utf8 ⇒ Boolean
Checks whether UTF-8 encoding is indicated in the LANG environment variable.
-
#initialize(name, utf8: default_utf8) ⇒ Tree
constructor
Initializes a new tree node with the given name, and UTF-8 support flag.
-
#to_ary ⇒ Array<String>
(also: #to_a)
Converts the tree node into an array representation.
-
#to_enum ⇒ Enumerator
Returns an enumerator that yields the tree node’s name, followed by its children in a hierarchical format.
-
#to_s ⇒ String
Returns the string representation of the tree structure.
Constructor Details
#initialize(name, utf8: default_utf8) ⇒ Tree
Initializes a new tree node with the given name, and UTF-8 support flag.
be used for display
156 157 158 159 160 |
# File 'lib/const_conf/tree.rb', line 156 def initialize(name, utf8: default_utf8) @name = name @utf8 = utf8 @children = [] end |
Class Method Details
.from_const_conf(configuration_or_setting) ⇒ ConstConf::Tree
Converts a ConstConf configuration or setting into a tree structure for display purposes.
This method takes either a ConstConf module or a specific setting and transforms it into a hierarchical tree representation that can be used for visualization or debugging. It delegates to specialized conversion methods based on the type of the input argument.
setting to convert
hierarchy
nor a ConstConf::Setting instance
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/const_conf/tree.rb', line 30 def from_const_conf(configuration_or_setting) if configuration?(configuration_or_setting) convert_module(configuration_or_setting) elsif configuration_or_setting.is_a?(ConstConf::Setting) convert_setting(configuration_or_setting) else raise ArgumentError, "argument needs to have type ConstConf::Setting or ConstConf" end end |
Instance Method Details
#<<(child) ⇒ Array<ConstConf::Tree>
Adds a child node to this tree node’s collection of children.
180 181 182 |
# File 'lib/const_conf/tree.rb', line 180 def <<(child) @children << child end |
#default_utf8 ⇒ Boolean
Checks whether UTF-8 encoding is indicated in the LANG environment variable.
This method examines the LANG environment variable to determine if it contains a UTF-8 encoding indicator, returning true if UTF-8 is detected and false otherwise.
encoding, false otherwise
171 172 173 |
# File 'lib/const_conf/tree.rb', line 171 def default_utf8 !!(ENV['LANG'] =~ /utf-8\z/i) end |
#to_ary ⇒ Array<String> Also known as: to_a
Converts the tree node into an array representation.
the tree node and its children
212 213 214 |
# File 'lib/const_conf/tree.rb', line 212 def to_ary to_enum.to_a end |
#to_enum ⇒ Enumerator
Returns an enumerator that yields the tree node’s name, followed by its children in a hierarchical format.
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/const_conf/tree.rb', line 189 def to_enum Enumerator.new do |y| y.yield @name @children.each_with_index do |child, child_index| children_enum = child.to_enum if child_index < @children.size - 1 children_enum.each_with_index do |setting, i| y.yield "#{inner_child_prefix(i)}#{setting}" end else children_enum.each_with_index do |setting, i| y.yield "#{last_child_prefix(i)}#{setting}" end end end end end |
#to_s ⇒ String
Returns the string representation of the tree structure.
This method converts the tree node and its children into a formatted string, where each node is represented on its own line with appropriate indentation to show the hierarchical relationship between nodes.
225 226 227 |
# File 'lib/const_conf/tree.rb', line 225 def to_s to_ary * ?\n end |