summaryrefslogtreecommitdiffstats
path: root/lib/puppet/parser/ast/objectref.rb
blob: e9561d65d2342c890ca9573ab3c999de1d86e96f (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Puppet::Parser::AST
    # A reference to an object.  Only valid as an rvalue.
    class ObjectRef < AST::Branch
        attr_accessor :name, :type

        def each
            [@type,@name].flatten.each { |param|
                #Puppet.debug("yielding param %s" % param)
                yield param
            }
        end

        # Evaluate our object, but just return a simple array of the type
        # and name.
        def evaluate(scope)
            objtype = @type.safeevaluate(scope)
            objnames = @name.safeevaluate(scope)

            # it's easier to always use an array, even for only one name
            unless objnames.is_a?(Array)
                objnames = [objnames]
            end

            # Verify we can find the object.
            begin
                object = scope.lookuptype(objtype)
            rescue Puppet::ParseError => except
                except.line = self.line
                except.file = self.file
                raise except
            rescue => detail
                error = Puppet::ParseError.new(detail)
                error.line = self.line
                error.file = self.file
                error.stack = caller
                raise error
            end
            Puppet.debug "ObjectRef returned type %s" % object

            # should we implicitly iterate here?
            # yes, i believe that we essentially have to...
            objnames.collect { |objname|
                if object.is_a?(AST::Component)
                    objname = "%s[%s]" % [objtype,objname]
                    objtype = "component"
                end
                [objtype,objname]
            }.reject { |obj| obj.nil? }
        end

        def tree(indent = 0)
            return [
                @type.tree(indent + 1),
                @name.tree(indent + 1),
                ((@@indline * indent) + self.typewrap(self.pin))
            ].join("\n")
        end

        def to_s
            return "%s[%s]" % [@name,@type]
        end
    end

end