Class: ComplexConfig::Tree

Inherits:
Object show all
Defined in:
lib/complex_config/tree.rb

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.

See Also:

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • name (Object)

    The name or key to use for the tree node

  • utf8 (Boolean) (defaults to: default_utf8)

    Whether to use UTF-8 characters for tree rendering, defaults to the result of default_utf8



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.

Parameters:

  • name (Object)

    The name or key to use for the tree node

  • value (Object)

    The configuration value to convert, which can be a ComplexConfig::Settings object, Hash, Array, or primitive value

Returns:

  • (ComplexConfig::Tree)

    A tree object representing the hierarchical structure of the configuration data



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

Parameters:

Returns:

  • (self)

    returns self to allow for method chaining after adding the child node



146
147
148
# File 'lib/complex_config/tree.rb', line 146

def <<(child)
  @children << child
end

#default_utf8TrueClass, 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.

Returns:

  • (TrueClass, FalseClass)

    true if the LANG environment variable indicates UTF-8 encoding, false otherwise



72
73
74
# File 'lib/complex_config/tree.rb', line 72

def default_utf8
  !!(ENV['LANG'] =~ /utf-8\z/i)
end

#to_aryArray<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.

Returns:

  • (Array<String>)

    an array of formatted strings representing the tree structure with proper indentation and visual connections between parent and child nodes



161
162
163
# File 'lib/complex_config/tree.rb', line 161

def to_ary
  to_enum.to_a
end

#to_enumEnumerator

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.

Returns:

  • (Enumerator)

    An enumerator that yields formatted tree node strings in a hierarchical format with proper indentation and connection characters



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_strString 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.

Returns:

  • (String)

    a newline-separated string containing all tree node representations



177
178
179
# File 'lib/complex_config/tree.rb', line 177

def to_str
  to_ary * ?\n
end