Class: ComplexConfig::Tree

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, utf8: default_utf8) ⇒ Tree

Returns a new instance of Tree.



28
29
30
31
32
# File 'lib/complex_config/tree.rb', line 28

def initialize(name, utf8: default_utf8)
  @name     = name
  @utf8     = utf8
  @children = []
end

Class Method Details

.convert(name, value) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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

Instance Method Details

#<<(child) ⇒ Object



77
78
79
# File 'lib/complex_config/tree.rb', line 77

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

#default_utf8Object



34
35
36
# File 'lib/complex_config/tree.rb', line 34

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

#to_aryObject Also known as: to_a



81
82
83
# File 'lib/complex_config/tree.rb', line 81

def to_ary
  to_enum.to_a
end

#to_enumObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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_strObject Also known as: to_s



87
88
89
# File 'lib/complex_config/tree.rb', line 87

def to_str
  to_ary * ?\n
end