Sha256: 5cc5bf65864ce5182526df61071210cb4a7fbc2cc8969d0f6229a7f4396ee84d

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

require 'astrails/safe/config/builder'
module Astrails
  module Safe
    module Config
      class Node
        def initialize(parent = nil, data = {}, &block)
          @parent, @data = parent, {}
          data.each { |k, v| self[k] = v }
          Builder.new(self).instance_eval(&block) if block
        end

        # looks for the path from this node DOWN. will not delegate to parent
        def get(*path)
          key = path.shift
          value = @data[key.to_s]
          return value if value && path.empty?

          value && value.get(*path)
        end

        # recursive find
        # starts at the node and continues to the parent
        def find(*path)
          get(*path) || @parent && @parent.find(*path)
        end
        alias :[] :find

        def set(key, value, &block)
          @data[key.to_s] =
            if value.is_a?(Hash)
              Node.new(self, value, &block)
            else
              raise(ArgumentError, "#{key}: no block supported for simple values") if block
              value
            end
        end
        alias :[]= :set

        def each(&block)
          @data.each(&block)
        end
        include Enumerable

        def dump(indent = "")
          @data.each do |key, value|
            if value.is_a?(Node)
              puts "#{indent}#{key}:"
              value.dump(indent + "    ")
            else
              puts "#{indent}#{key}: #{value.inspect}"
            end
          end
        end

      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
astrails-safe-0.0.8 lib/astrails/safe/config/node.rb
astrails-safe-0.0.9 lib/astrails/safe/config/node.rb
astrails-safe-0.1.0 lib/astrails/safe/config/node.rb