Class: ConstConf::Tree

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

Parameters:

  • name (String)

    the name of the tree node

  • utf8 (Boolean) (defaults to: default_utf8)

    flag indicating whether UTF-8 characters should



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

Parameters:

  • configuration_or_setting (Object)

    the ConstConf module or

Returns:

Raises:

  • (ArgumentError)

    if the argument is neither a ConstConf module



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.

Parameters:

Returns:



180
181
182
# File 'lib/const_conf/tree.rb', line 180

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

#default_utf8Boolean

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

Returns:

  • (Boolean)

    true if the LANG environment variable indicates UTF-8



171
172
173
# File 'lib/const_conf/tree.rb', line 171

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

#to_aryArray<String> Also known as: to_a

Converts the tree node into an array representation.

the tree node and its children

Returns:

  • (Array<String>)

    an array containing the string representations of



212
213
214
# File 'lib/const_conf/tree.rb', line 212

def to_ary
  to_enum.to_a
end

#to_enumEnumerator

Returns an enumerator that yields the tree node’s name, followed by its children in a hierarchical format.

Returns:

  • (Enumerator)

    an enumerator that provides the tree structure as a sequence of strings representing nodes and their relationships



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_sString

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.

Returns:

  • (String)

    a multi-line string representation of the tree structure



225
226
227
# File 'lib/const_conf/tree.rb', line 225

def to_s
  to_ary * ?\n
end