Class: Puppet::Parser::TemplateWrapper Private

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/puppet/parser/templatewrapper.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A simple wrapper for templates, so they don’t have full access to the scope objects.

Constant Summary

Constants included from Util

Util::ALNUM, Util::ALPHA, Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE, Util::ESCAPED, Util::HEX, Util::HttpProxy, Util::PUPPET_STACK_INSERTION_FRAME, Util::RESERVED, Util::RFC_3986_URI_REGEX, Util::UNRESERVED, Util::UNSAFE

Constants included from Util::POSIX

Util::POSIX::LOCALE_ENV_VARS, Util::POSIX::USER_ENV_VARS

Constants included from Util::SymbolicFileMode

Util::SymbolicFileMode::SetGIDBit, Util::SymbolicFileMode::SetUIDBit, Util::SymbolicFileMode::StickyBit, Util::SymbolicFileMode::SymbolicMode, Util::SymbolicFileMode::SymbolicSpecialToBit

Instance Method Summary collapse

Methods included from Util

absolute_path?, benchmark, chuser, clear_environment, create_erb, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, format_backtrace_array, format_puppetstack_frame, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, resolve_stackframe, rfc2396_escape, safe_posix_fork, set_env, skip_external_facts, symbolizehash, thinmark, uri_encode, uri_query_encode, uri_to_path, uri_unescape, which, withenv, withumask

Methods included from Util::POSIX

#get_posix_field, #gid, groups_of, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Methods included from Util::SymbolicFileMode

#display_mode, #normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Constructor Details

#initialize(scope) ⇒ TemplateWrapper

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of TemplateWrapper.



15
16
17
# File 'lib/puppet/parser/templatewrapper.rb', line 15

def initialize(scope)
  @__scope__ = scope
end

Instance Method Details

#all_tagsArray<String>

Returns All the defined tags.

Returns:

  • (Array<String>)

    All the defined tags



60
61
62
# File 'lib/puppet/parser/templatewrapper.rb', line 60

def all_tags
  scope.catalog.tags
end

#classesArray<String>

Returns The list of defined classes.

Returns:

  • (Array<String>)

    The list of defined classes



48
49
50
# File 'lib/puppet/parser/templatewrapper.rb', line 48

def classes
  scope.catalog.classes
end

#fileString

Returns The full path name of the template that is being executed.

Returns:

  • (String)

    The full path name of the template that is being executed



21
22
23
# File 'lib/puppet/parser/templatewrapper.rb', line 21

def file
  @__file__
end

#file=(filename) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



65
66
67
68
69
70
# File 'lib/puppet/parser/templatewrapper.rb', line 65

def file=(filename)
  @__file__ = Puppet::Parser::Files.find_template(filename, scope.compiler.environment)
  unless @__file__
    raise Puppet::ParseError, _("Could not find template '%{filename}'") % { filename: filename }
  end
end

#has_variable?(name) ⇒ Boolean

Should return true if a variable is defined, false if it is not

Returns:

  • (Boolean)


42
43
44
# File 'lib/puppet/parser/templatewrapper.rb', line 42

def has_variable?(name)
  scope.include?(name.to_s)
end

#result(string = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/puppet/parser/templatewrapper.rb', line 73

def result(string = nil)
  if string
    template_source = "inline template"
  else
    string = Puppet::FileSystem.read_preserve_line_endings(@__file__)
    template_source = @__file__
  end

  # Expose all the variables in our scope as instance variables of the
  # current object, making it possible to access them without conflict
  # to the regular methods.
  escaped_template_source = template_source.gsub(/%/, '%%')
  benchmark(:debug, _("Bound template variables for %{template_source} in %%{seconds} seconds") % { template_source: escaped_template_source }) do
    scope.to_hash.each do |name, value|
      realname = name.gsub(/[^\w]/, "_")
      instance_variable_set("@#{realname}", value)
    end
  end

  result = nil
  benchmark(:debug, _("Interpolated template %{template_source} in %%{seconds} seconds") % { template_source: escaped_template_source }) do
    template = Puppet::Util.create_erb(string)
    template.filename = @__file__
    result = template.result(binding)
  end

  result
end

#scopePuppet::Parser::Scope

Returns The scope in which the template is evaluated.

Returns:



27
28
29
# File 'lib/puppet/parser/templatewrapper.rb', line 27

def scope
  @__scope__
end

#tagsArray<String>

Returns The tags defined in the current scope.

Returns:

  • (Array<String>)

    The tags defined in the current scope

Raises:

  • (NotImplementedError)


54
55
56
# File 'lib/puppet/parser/templatewrapper.rb', line 54

def tags
  raise NotImplementedError, "Call 'all_tags' instead."
end

#to_sObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



102
103
104
# File 'lib/puppet/parser/templatewrapper.rb', line 102

def to_s
  "template[#{@__file__ || 'inline'}]"
end