Class: Puppet::Pops::Lookup::Invocation Private

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pops/lookup/invocation.rb

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.

Direct Known Subclasses

ScopeLookupCollectingInvocation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, override_values = EMPTY_HASH, default_values = EMPTY_HASH, explainer = nil, adapter_class = nil) ⇒ Invocation

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.

Creates a context object for a lookup invocation. The object contains the current scope, overrides, and default values and may optionally contain an ExplanationAcceptor instance that will receive book-keeping information about the progress of the lookup.

If the explain argument is a boolean, then false means that no explanation is needed and true means that the default explanation acceptor should be used. The explain argument may also be an instance of the ‘ExplanationAcceptor` class.

Parameters:

  • scope (Puppet::Parser::Scope)

    The scope to use for the lookup

  • override_values (Hash<String,Object>|nil) (defaults to: EMPTY_HASH)

    A map to use as override. Values found here are returned immediately (no merge)

  • default_values (Hash<String,Object>) (defaults to: EMPTY_HASH)

    A map to use as the last resort (but before default)

  • explainer (boolean, Explanainer) (defaults to: nil)

    An boolean true to use the default explanation acceptor or an explainer instance that will receive information about the lookup



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/puppet/pops/lookup/invocation.rb', line 42

def initialize(scope, override_values = EMPTY_HASH, default_values = EMPTY_HASH, explainer = nil, adapter_class = nil)
  @scope = scope
  @override_values = override_values
  @default_values = default_values

  parent_invocation = self.class.current

  if parent_invocation && (adapter_class.nil? || adapter_class == parent_invocation.adapter_class)
    # Inherit from parent invocation (track recursion)
    @name_stack = parent_invocation.name_stack
    @adapter_class = parent_invocation.adapter_class

    # Inherit Hiera 3 legacy properties
    set_hiera_xxx_call if parent_invocation.hiera_xxx_call?
    set_hiera_v3_merge_behavior if parent_invocation.hiera_v3_merge_behavior?
    set_global_only if parent_invocation.global_only?
    povr = parent_invocation.hiera_v3_location_overrides
    set_hiera_v3_location_overrides(povr) unless povr.empty?

    # Inherit explainer unless a new explainer is given or disabled using false
    explainer = explainer == false ? nil : parent_invocation.explainer
  else
    @name_stack = []
    @adapter_class = adapter_class.nil? ? LookupAdapter : adapter_class
    unless explainer.is_a?(Explainer)
      explainer = explainer == true ? Explainer.new : nil
    end
    explainer = DebugExplainer.new(explainer) if Puppet[:debug] && !explainer.is_a?(DebugExplainer)
  end
  @explainer = explainer
end

Instance Attribute Details

#adapter_classObject (readonly)

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.



11
12
13
# File 'lib/puppet/pops/lookup/invocation.rb', line 11

def adapter_class
  @adapter_class
end

#default_valuesObject (readonly)

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.



11
12
13
# File 'lib/puppet/pops/lookup/invocation.rb', line 11

def default_values
  @default_values
end

#explainerObject (readonly)

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.



11
12
13
# File 'lib/puppet/pops/lookup/invocation.rb', line 11

def explainer
  @explainer
end

#module_nameObject (readonly)

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.



11
12
13
# File 'lib/puppet/pops/lookup/invocation.rb', line 11

def module_name
  @module_name
end

#override_valuesObject (readonly)

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.



11
12
13
# File 'lib/puppet/pops/lookup/invocation.rb', line 11

def override_values
  @override_values
end

#scopeObject (readonly)

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.



11
12
13
# File 'lib/puppet/pops/lookup/invocation.rb', line 11

def scope
  @scope
end

#top_keyObject (readonly)

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.



11
12
13
# File 'lib/puppet/pops/lookup/invocation.rb', line 11

def top_key
  @top_key
end

Class Method Details

.currentObject

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.



13
14
15
# File 'lib/puppet/pops/lookup/invocation.rb', line 13

def self.current
  (@current ||= Puppet::ThreadLocal.new(nil)).value
end

.current=(new_value) ⇒ 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.



17
18
19
# File 'lib/puppet/pops/lookup/invocation.rb', line 17

def self.current=(new_value)
  @current.value = new_value
end

Instance Method Details

#check(name) ⇒ 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.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/puppet/pops/lookup/invocation.rb', line 91

def check(name)
  if @name_stack.include?(name)
    raise Puppet::DataBinding::RecursiveLookupError, _("Recursive lookup detected in [%{name_stack}]") % { name_stack: @name_stack.join(', ') }
  end
  return unless block_given?

  @name_stack.push(name)
  begin
    yield
  rescue Puppet::DataBinding::LookupError
    raise
  rescue Puppet::Error => detail
    raise Puppet::DataBinding::LookupError.new(detail.message, nil, nil, nil, detail)
  ensure
    @name_stack.pop
  end
end

#emit_debug_info(preamble) ⇒ 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.



109
110
111
# File 'lib/puppet/pops/lookup/invocation.rb', line 109

def emit_debug_info(preamble)
  @explainer.emit_debug_info(preamble) if @explainer.is_a?(DebugExplainer)
end

#explain_options?Boolean

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:

  • (Boolean)


169
170
171
# File 'lib/puppet/pops/lookup/invocation.rb', line 169

def explain_options?
  @explainer.nil? ? false : @explainer.explain_options?
end

#global_hiera_config_pathPathname

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 the full path of the hiera.yaml config file.

Returns:

  • (Pathname)

    the full path of the hiera.yaml config file



234
235
236
# File 'lib/puppet/pops/lookup/invocation.rb', line 234

def global_hiera_config_path
  lookup_adapter.global_hiera_config_path
end

#global_only?Boolean

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:

  • (Boolean)


222
223
224
# File 'lib/puppet/pops/lookup/invocation.rb', line 222

def global_only?
  lookup_adapter.global_only? || (instance_variable_defined?(:@global_only) ? @global_only : false)
end

#hiera_v3_location_overridesObject

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.



261
262
263
# File 'lib/puppet/pops/lookup/invocation.rb', line 261

def hiera_v3_location_overrides
  instance_variable_defined?(:@hiera_v3_location_overrides) ? @hiera_v3_location_overrides : EMPTY_ARRAY
end

#hiera_v3_merge_behavior?Boolean

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 ‘true` if the invocation stems from the hiera_xxx function family.

Returns:

  • (Boolean)

    ‘true` if the invocation stems from the hiera_xxx function family



248
249
250
# File 'lib/puppet/pops/lookup/invocation.rb', line 248

def hiera_v3_merge_behavior?
  instance_variable_defined?(:@hiera_v3_merge_behavior)
end

#hiera_xxx_call?Boolean

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 ‘true` if the invocation stems from the hiera_xxx function family.

Returns:

  • (Boolean)

    ‘true` if the invocation stems from the hiera_xxx function family



239
240
241
# File 'lib/puppet/pops/lookup/invocation.rb', line 239

def hiera_xxx_call?
  instance_variable_defined?(:@hiera_xxx_call)
end

#lookup(key, module_name = 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.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/puppet/pops/lookup/invocation.rb', line 74

def lookup(key, module_name = nil)
  key = LookupKey.new(key) unless key.is_a?(LookupKey)
  @top_key = key
  @module_name = module_name.nil? ? key.module_name : module_name
  save_current = self.class.current
  if save_current.equal?(self)
    yield
  else
    begin
      self.class.current = self
      yield
    ensure
      self.class.current = save_current
    end
  end
end

#lookup_adapterObject

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.

rubocop:disable Naming/MemoizedInstanceVariableName



114
115
116
# File 'lib/puppet/pops/lookup/invocation.rb', line 114

def lookup_adapter
  @adapter ||= @adapter_class.adapt(scope.compiler)
end

#only_explain_options?Boolean

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:

  • (Boolean)


165
166
167
# File 'lib/puppet/pops/lookup/invocation.rb', line 165

def only_explain_options?
  @explainer.nil? ? false : @explainer.only_explain_options?
end

#remember_scope_lookup(*lookup_result) ⇒ 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.

This method is overridden by the special Invocation used while resolving interpolations in a Hiera configuration file (hiera.yaml) where it’s used for collecting and remembering the current values that the configuration was based on



124
125
126
# File 'lib/puppet/pops/lookup/invocation.rb', line 124

def remember_scope_lookup(*lookup_result)
  # Does nothing by default
end

#report_found(key, value) ⇒ 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.



183
184
185
186
# File 'lib/puppet/pops/lookup/invocation.rb', line 183

def report_found(key, value)
  @explainer.accept_found(key, value) unless @explainer.nil?
  value
end

#report_found_in_defaults(key, value) ⇒ 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.



178
179
180
181
# File 'lib/puppet/pops/lookup/invocation.rb', line 178

def report_found_in_defaults(key, value)
  @explainer.accept_found_in_defaults(key, value) unless @explainer.nil?
  value
end

#report_found_in_overrides(key, value) ⇒ 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.



173
174
175
176
# File 'lib/puppet/pops/lookup/invocation.rb', line 173

def report_found_in_overrides(key, value)
  @explainer.accept_found_in_overrides(key, value) unless @explainer.nil?
  value
end

#report_location_not_foundObject

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.



204
205
206
# File 'lib/puppet/pops/lookup/invocation.rb', line 204

def report_location_not_found
  @explainer.accept_location_not_found unless @explainer.nil?
end

#report_merge_source(merge_source) ⇒ 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.



188
189
190
# File 'lib/puppet/pops/lookup/invocation.rb', line 188

def report_merge_source(merge_source)
  @explainer.accept_merge_source(merge_source) unless @explainer.nil?
end

#report_module_not_found(module_name) ⇒ 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.



208
209
210
# File 'lib/puppet/pops/lookup/invocation.rb', line 208

def report_module_not_found(module_name)
  @explainer.accept_module_not_found(module_name) unless @explainer.nil?
end

#report_module_provider_not_found(module_name) ⇒ 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.



212
213
214
# File 'lib/puppet/pops/lookup/invocation.rb', line 212

def report_module_provider_not_found(module_name)
  @explainer.accept_module_provider_not_found(module_name) unless @explainer.nil?
end

#report_not_found(key) ⇒ 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.



200
201
202
# File 'lib/puppet/pops/lookup/invocation.rb', line 200

def report_not_found(key)
  @explainer.accept_not_found(key) unless @explainer.nil?
end

#report_result(value) ⇒ 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.

Report the result of a merge or fully resolved interpolated string

Parameters:

  • value (Object)

    The result to report

Returns:

  • (Object)

    the given value



195
196
197
198
# File 'lib/puppet/pops/lookup/invocation.rb', line 195

def report_result(value)
  @explainer.accept_result(value) unless @explainer.nil?
  value
end

#report_text(&block) ⇒ 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.



216
217
218
219
220
# File 'lib/puppet/pops/lookup/invocation.rb', line 216

def report_text(&block)
  unless @explainer.nil?
    @explainer.accept_text(block.call)
  end
end

#set_global_onlyInvocation

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.

Instructs the lookup framework to only perform lookups in the global layer

Returns:



228
229
230
231
# File 'lib/puppet/pops/lookup/invocation.rb', line 228

def set_global_only
  @global_only = true
  self
end

#set_hiera_v3_location_overrides(overrides) ⇒ 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.

Overrides passed from hiera_xxx functions down to V3DataHashFunctionProvider



257
258
259
# File 'lib/puppet/pops/lookup/invocation.rb', line 257

def set_hiera_v3_location_overrides(overrides)
  @hiera_v3_location_overrides = [overrides].flatten unless overrides.nil?
end

#set_hiera_v3_merge_behaviorObject

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.



252
253
254
# File 'lib/puppet/pops/lookup/invocation.rb', line 252

def set_hiera_v3_merge_behavior
  @hiera_v3_merge_behavior = true
end

#set_hiera_xxx_callObject

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.



243
244
245
# File 'lib/puppet/pops/lookup/invocation.rb', line 243

def set_hiera_xxx_call
  @hiera_xxx_call = true
end

#with(qualifier_type, qualifier) ⇒ 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.

The qualifier_type can be one of: :global - qualifier is the data binding terminus name :data_provider - qualifier a DataProvider instance :path - qualifier is a ResolvedPath instance :merge - qualifier is a MergeStrategy instance :interpolation - qualifier is the unresolved interpolation expression :meta - qualifier is the module name :data - qualifier is the key

Parameters:

  • qualifier (Object)

    A branch, a provider, or a path



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/puppet/pops/lookup/invocation.rb', line 138

def with(qualifier_type, qualifier)
  if explainer.nil?
    yield
  else
    @explainer.push(qualifier_type, qualifier)
    begin
      yield
    ensure
      @explainer.pop
    end
  end
end

#with_scope(scope) {|Invocation.new(scope, override_values, default_values, explainer)| ... } ⇒ Invocation

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.

Creates a new instance with same settings as this instance but with a new given scope and yields with that scope.

Parameters:

Yields:

Returns:



26
27
28
# File 'lib/puppet/pops/lookup/invocation.rb', line 26

def with_scope(scope)
  yield(Invocation.new(scope, override_values, default_values, explainer))
end

#without_explainObject

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.



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/puppet/pops/lookup/invocation.rb', line 151

def without_explain
  if explainer.nil?
    yield
  else
    save_explainer = @explainer
    begin
      @explainer = nil
      yield
    ensure
      @explainer = save_explainer
    end
  end
end