summaryrefslogtreecommitdiffstats
path: root/lib/puppet/parser/ast/asthash.rb
blob: ae81d35ddb565a4398b5b35aeeffae360b67ed6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
require 'puppet/parser/ast/leaf'

class Puppet::Parser::AST
  class ASTHash < Leaf
    include Enumerable

    # Evaluate our children.
    def evaluate(scope)
      items = {}

      @value.each_pair do |k,v|
        key = k.respond_to?(:safeevaluate) ? k.safeevaluate(scope) : k
        items.merge!({ key => v.safeevaluate(scope) })
      end

      items
    end

    def merge(hash)
      case hash
      when ASTHash
        @value = @value.merge(hash.value)
      when Hash
        @value = @value.merge(hash)
      end
    end

    def to_s
      "{" + @value.collect { |v| v.collect { |a| a.to_s }.join(' => ') }.join(', ') + "}"
    end

    def initialize(args)
      super(args)
      @value ||= {}
    end
  end
end