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::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE, Util::RFC_3986_URI_REGEX

Constants included from Util::SymbolicFileMode

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

Constants included from Util::POSIX

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

Instance Method Summary collapse

Methods included from Util

absolute_path?, benchmark, chuser, clear_environment, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, safe_posix_fork, set_env, symbolizehash, thinmark, uri_encode, uri_query_encode, uri_to_path, which, withenv, withumask

Methods included from Util::SymbolicFileMode

#normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Methods included from Util::POSIX

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

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


13
14
15
# File 'lib/puppet/parser/templatewrapper.rb', line 13

def initialize(scope)
  @__scope__ = scope
end

Instance Method Details

#all_tagsArray<String>

Returns All the defined tags


58
59
60
# File 'lib/puppet/parser/templatewrapper.rb', line 58

def all_tags
  scope.catalog.tags
end

#classesArray<String>

Returns The list of defined classes


46
47
48
# File 'lib/puppet/parser/templatewrapper.rb', line 46

def classes
  scope.catalog.classes
end

#fileString

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


19
20
21
# File 'lib/puppet/parser/templatewrapper.rb', line 19

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.


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

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


40
41
42
# File 'lib/puppet/parser/templatewrapper.rb', line 40

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.


71
72
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
# File 'lib/puppet/parser/templatewrapper.rb', line 71

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 = ERB.new(string, 0, "-")
    template.filename = @__file__
    result = template.result(binding)
  end

  result
end

#scopePuppet::Parser::Scope

Returns The scope in which the template is evaluated


25
26
27
# File 'lib/puppet/parser/templatewrapper.rb', line 25

def scope
  @__scope__
end

#tagsArray<String>

Returns The tags defined in the current scope


52
53
54
# File 'lib/puppet/parser/templatewrapper.rb', line 52

def tags
  scope.tags
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.


100
101
102
# File 'lib/puppet/parser/templatewrapper.rb', line 100

def to_s
  "template[#{(@__file__ ? @__file__ : "inline")}]"
end