class Hiera::Scope

Constants

CALLING_CLASS
CALLING_CLASS_PATH
CALLING_KEYS
CALLING_MODULE
EMPTY_STRING
MODULE_NAME

Attributes

real[R]

Public Class Methods

new(real) click to toggle source
   # File lib/hiera/scope.rb
16 def initialize(real)
17   @real = real
18 end

Public Instance Methods

[](key) click to toggle source
   # File lib/hiera/scope.rb
20 def [](key)
21   if key == CALLING_CLASS
22     ans = find_hostclass(@real)
23   elsif key == CALLING_CLASS_PATH
24     ans = find_hostclass(@real).gsub(/::/, '/')
25   elsif key == CALLING_MODULE
26     ans = safe_lookupvar(MODULE_NAME)
27   else
28     ans = safe_lookupvar(key)
29   end
30   ans == EMPTY_STRING ? nil : ans
31 end
catalog() click to toggle source
   # File lib/hiera/scope.rb
63 def catalog
64   @real.catalog
65 end
compiler() click to toggle source
   # File lib/hiera/scope.rb
71 def compiler
72   @real.compiler
73 end
exist?(key) click to toggle source
   # File lib/hiera/scope.rb
55 def exist?(key)
56   CALLING_KEYS.include?(key) || @real.exist?(key)
57 end
include?(key) click to toggle source
   # File lib/hiera/scope.rb
59 def include?(key)
60   CALLING_KEYS.include?(key) || @real.include?(key)
61 end
resource() click to toggle source
   # File lib/hiera/scope.rb
67 def resource
68   @real.resource
69 end

Private Instance Methods

find_hostclass(scope) click to toggle source
   # File lib/hiera/scope.rb
75 def find_hostclass(scope)
76   if scope.source and scope.source.type == :hostclass
77     return scope.source.name.downcase
78   elsif scope.parent
79     return find_hostclass(scope.parent)
80   else
81     return nil
82   end
83 end
safe_lookupvar(key) click to toggle source

This method is used to handle the throw of :undefined_variable since when strict variables is not in effect, missing handling of the throw leads to a more expensive code path.

   # File lib/hiera/scope.rb
37 def safe_lookupvar(key)
38   reason = catch :undefined_variable do
39     return @real.lookupvar(key)
40   end
41 
42   case Puppet[:strict]
43   when :off
44     # do nothing
45   when :warning
46     Puppet.warn_once(Puppet::Parser::Scope::UNDEFINED_VARIABLES_KIND, _("Variable: %{name}") % { name: key },
47     _("Undefined variable '%{name}'; %{reason}") % { name: key, reason: reason } )
48   when :error
49     raise ArgumentError, _("Undefined variable '%{name}'; %{reason}") % { name: key, reason: reason }
50   end
51   nil
52 end