Sha256: 8986b996948205ef570c4c30b1bd7db6d217dbb73869d76c4db5bc23393bce3c

Contents?: true

Size: 1.79 KB

Versions: 3

Compression:

Stored size: 1.79 KB

Contents

module ComplexConfig
  class Tree
    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

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

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

    private

    def inner_child_prefix(i)
      if @utf8
        i.zero? ? "├─ " : "│  "
      else
        i.zero? ? "+- " : "|  "
      end
    end

    def last_child_prefix(i)
      if @utf8
        i.zero? ? "└─ " : "   "
      else
        i.zero? ? "`- " : "   "
      end
    end

    public

    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

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

    def to_ary
      to_enum.to_a
    end

    alias to_a to_ary

    def to_str
      to_ary * ?\n
    end

    alias to_s to_str
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
complex_config-0.22.2 lib/complex_config/tree.rb
complex_config-0.22.1 lib/complex_config/tree.rb
complex_config-0.22.0 lib/complex_config/tree.rb