Class: ComplexConfig::Tree
Overview
A tree class that provides hierarchical representation of configuration data
The Tree class is used to create visual tree structures from configuration data, allowing for easy display and understanding of nested configuration settings. It supports both UTF-8 and ASCII character sets for rendering tree connections and can handle various data types including Settings objects, hashes, arrays, and primitive values.
Class Method Summary collapse
-
.convert(name, value) ⇒ ComplexConfig::Tree
The convert method transforms configuration data into a tree structure.
Instance Method Summary collapse
-
#<<(child) ⇒ self
The << method appends a child node to the tree structure.
-
#default_utf8 ⇒ TrueClass, FalseClass
The default_utf8 method determines whether UTF-8 character support should be enabled.
-
#initialize(name, utf8: default_utf8) ⇒ Tree
constructor
The initialize method sets up a new tree node with the specified name and UTF-8 support configuration.
-
#to_ary ⇒ Array<String>
(also: #to_a)
The to_ary method converts the tree structure to an array representation.
-
#to_enum ⇒ Enumerator
The to_enum method creates an enumerator for tree traversal.
-
#to_str ⇒ String
(also: #to_s)
The to_str method converts the tree structure to a string representation.
Constructor Details
#initialize(name, utf8: default_utf8) ⇒ Tree
The initialize method sets up a new tree node with the specified name and UTF-8 support configuration.
58 59 60 61 62 |
# File 'lib/complex_config/tree.rb', line 58 def initialize(name, utf8: default_utf8) @name = name @utf8 = utf8 @children = [] end |
Class Method Details
.convert(name, value) ⇒ ComplexConfig::Tree
The convert method transforms configuration data into a tree structure
This method recursively processes configuration values and converts them into a hierarchical tree representation. It handles different value types including ComplexConfig::Settings objects, Hashes, Arrays, and primitive values, creating appropriate tree nodes for each case.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/complex_config/tree.rb', line 27 def self.convert(name, value) case value when ComplexConfig::Settings convert(name, value.to_h) when Hash obj = new(name.to_s) value.each do |name, value| obj << convert(name, value) end obj when Array obj = new(name.to_s) value.each_with_index do |value, i| obj << convert(i, value) end obj else if name.is_a?(Integer) new value.inspect else new "#{name} = #{value.inspect}" end end end |
Instance Method Details
#<<(child) ⇒ self
The << method appends a child node to the tree structure
146 147 148 |
# File 'lib/complex_config/tree.rb', line 146 def <<(child) @children << child end |
#default_utf8 ⇒ TrueClass, FalseClass
The default_utf8 method determines whether UTF-8 character support should be enabled
This method checks the LANG environment variable to determine if UTF-8 encoding should be used for tree rendering.
72 73 74 |
# File 'lib/complex_config/tree.rb', line 72 def default_utf8 !!(ENV['LANG'] =~ /utf-8\z/i) end |
#to_ary ⇒ Array<String> Also known as: to_a
The to_ary method converts the tree structure to an array representation
This method generates an array containing string representations of all nodes in the tree structure, including their hierarchical relationships and proper indentation with connection characters. It delegates to the to_enum method to produce the underlying enumeration before converting it to an array.
161 162 163 |
# File 'lib/complex_config/tree.rb', line 161 def to_ary to_enum.to_a end |
#to_enum ⇒ Enumerator
The to_enum method creates an enumerator for tree traversal
This method generates an Enumerator that yields string representations of the tree structure, including node names and their hierarchical relationships. It processes children recursively and applies appropriate prefix characters based on UTF-8 support and child position to visually represent the tree structure.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/complex_config/tree.rb', line 121 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_str ⇒ String Also known as: to_s
The to_str method converts the tree structure to a string representation
This method generates a string by joining all node representations in the tree with newline characters, providing a flat text representation of the hierarchical structure.
177 178 179 |
# File 'lib/complex_config/tree.rb', line 177 def to_str to_ary * ?\n end |