Class: Puppet::Pops::Lookup::Context

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Types::PuppetObject
Defined in:
lib/puppet/pops/lookup/context.rb

Overview

The Context is created once for each call to a function. It provides a combination of the Invocation object needed to provide explanation support and the FunctionContext object needed to provide the private cache. The Context is part of the public API. It will be passed to a data_hash, data_dig, or lookup_key function and its attributes and methods can be used in a Puppet function as well as in a Ruby function. The Context is maps to the Pcore type ‘Puppet::LookupContext’

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Types::PuppetObject

#_pcore_all_contents, #_pcore_contents, #_pcore_init_hash, #_pcore_type, #to_s

Constructor Details

#initialize(function_context, lookup_invocation) ⇒ Context

Returns a new instance of Context.



179
180
181
182
# File 'lib/puppet/pops/lookup/context.rb', line 179

def initialize(function_context, lookup_invocation)
  @lookup_invocation = lookup_invocation
  @function_context = function_context
end

Class Method Details

._pcore_typeObject



130
131
132
# File 'lib/puppet/pops/lookup/context.rb', line 130

def self._pcore_type
  @type
end

.from_asserted_args(module_name) ⇒ Object

Mainly for test purposes. Makes it possible to create a Puppet::Pops::Lookup::Context in Puppet code provided that a current Invocation exists.



172
173
174
# File 'lib/puppet/pops/lookup/context.rb', line 172

def self.from_asserted_args(module_name)
  new(FunctionContext.new(EnvironmentContext.adapt(Puppet.lookup(:environments).get(Puppet[:environment])), module_name, nil), Invocation.current)
end

.register_ptype(loader, ir) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/puppet/pops/lookup/context.rb', line 134

def self.register_ptype(loader, ir)
  tf = Types::TypeFactory
  key_type = tf.optional(tf.scalar)
  @type =
    Pcore.create_object_type(
      loader,
      ir,
      self,
      'Puppet::LookupContext',
      'Any',
      {
        'environment_name' => {
          Types::KEY_TYPE => Types::PStringType::NON_EMPTY,
          Types::KEY_KIND => Types::PObjectType::ATTRIBUTE_KIND_DERIVED
        },
        'module_name' => {
          Types::KEY_TYPE => tf.variant(Types::PStringType::NON_EMPTY, Types::PUndefType::DEFAULT)
        }
      },
      {
        'not_found' => tf.callable([0, 0], tf.undef),
        'explain' => tf.callable([0, 0, tf.callable(0, 0)], tf.undef),
        'interpolate' => tf.callable(1, 1),
        'cache' => tf.callable([key_type, tf.any], tf.any),
        'cache_all' => tf.callable([tf.hash_kv(key_type, tf.any)], tf.undef),
        'cache_has_key' => tf.callable([key_type], tf.boolean),
        'cached_value' => tf.callable([key_type], tf.any),
        'cached_entries' => tf.variant(
          tf.callable([0, 0, tf.callable(1, 1)], tf.undef),
          tf.callable([0, 0, tf.callable(2, 2)], tf.undef),
          tf.callable([0, 0], tf.iterable(tf.tuple([key_type, tf.any])))
        ),
        'cached_file_data' => tf.callable(tf.string, tf.optional(tf.callable([1, 1])))
      }
    ).resolve(loader)
end

Instance Method Details

#explain(&block) ⇒ Object

Will call the given block to obtain a textual explanation if explanation support is active.



186
187
188
189
# File 'lib/puppet/pops/lookup/context.rb', line 186

def explain(&block)
  @lookup_invocation.report_text(&block)
  nil
end

#interpolate(value) ⇒ Object

Resolve interpolation expressions in the given value

Parameters:

Returns:

  • (Object)

    the value with all interpolation expressions resolved



194
195
196
# File 'lib/puppet/pops/lookup/context.rb', line 194

def interpolate(value)
  @function_context.interpolate(value, @lookup_invocation, true)
end

#invocationObject

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.



203
204
205
# File 'lib/puppet/pops/lookup/context.rb', line 203

def invocation
  @lookup_invocation
end

#not_foundObject



198
199
200
# File 'lib/puppet/pops/lookup/context.rb', line 198

def not_found
  throw :no_such_key
end