class ComplexConfig::Tree

Public Class Methods

convert(name, value) click to toggle source
# File lib/complex_config/tree.rb, line 3
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
new(name, utf8: default_utf8) click to toggle source
# File lib/complex_config/tree.rb, line 28
def initialize(name, utf8: default_utf8)
  @name     = name
  @utf8     = utf8
  @children = []
end

Public Instance Methods

<<(child) click to toggle source
# File lib/complex_config/tree.rb, line 77
def <<(child)
  @children << child
end
default_utf8() click to toggle source
# File lib/complex_config/tree.rb, line 34
def default_utf8
  !!(ENV['LANG'] =~ /utf-8\z/i)
end
to_a()
Alias for: to_ary
to_ary() click to toggle source
# File lib/complex_config/tree.rb, line 81
def to_ary
  to_enum.to_a
end
Also aliased as: to_a
to_enum() click to toggle source
# File lib/complex_config/tree.rb, line 58
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()
Alias for: to_str
to_str() click to toggle source
# File lib/complex_config/tree.rb, line 87
def to_str
  to_ary * ?\n
end
Also aliased as: to_s

Private Instance Methods

inner_child_prefix(i) click to toggle source
# File lib/complex_config/tree.rb, line 40
def inner_child_prefix(i)
  if @utf8
    i.zero? ? "├─ " : "│  "
  else
    i.zero? ? "+- " : "|  "
  end
end
last_child_prefix(i) click to toggle source
# File lib/complex_config/tree.rb, line 48
def last_child_prefix(i)
  if @utf8
    i.zero? ? "└─ " : "   "
  else
    i.zero? ? "`- " : "   "
  end
end