class Puppet::Pops::Lookup::Context
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'
@api public
Public Class Methods
# File lib/puppet/pops/lookup/context.rb 129 def self._pcore_type 130 @type 131 end
Mainly for test purposes. Makes it possible to create a {Context} in Puppet
code provided that a current {Invocation} exists.
# File lib/puppet/pops/lookup/context.rb 164 def self.from_asserted_args(module_name) 165 new(FunctionContext.new(EnvironmentContext.adapt(Puppet.lookup(:environments).get(Puppet[:environment])), module_name, nil), Invocation.current) 166 end
# File lib/puppet/pops/lookup/context.rb 171 def initialize(function_context, lookup_invocation) 172 @lookup_invocation = lookup_invocation 173 @function_context = function_context 174 end
# File lib/puppet/pops/lookup/context.rb 133 def self.register_ptype(loader, ir) 134 tf = Types::TypeFactory 135 key_type = tf.optional(tf.scalar) 136 @type = Pcore::create_object_type(loader, ir, self, 'Puppet::LookupContext', 'Any', 137 { 138 'environment_name' => { 139 Types::KEY_TYPE => Types::PStringType::NON_EMPTY, 140 Types::KEY_KIND => Types::PObjectType::ATTRIBUTE_KIND_DERIVED 141 }, 142 'module_name' => { 143 Types::KEY_TYPE => tf.variant(Types::PStringType::NON_EMPTY, Types::PUndefType::DEFAULT) 144 } 145 }, 146 { 147 'not_found' => tf.callable([0, 0], tf.undef), 148 'explain' => tf.callable([0, 0, tf.callable(0,0)], tf.undef), 149 'interpolate' => tf.callable(1, 1), 150 'cache' => tf.callable([key_type, tf.any], tf.any), 151 'cache_all' => tf.callable([tf.hash_kv(key_type, tf.any)], tf.undef), 152 'cache_has_key' => tf.callable([key_type], tf.boolean), 153 'cached_value' => tf.callable([key_type], tf.any), 154 'cached_entries' => tf.variant( 155 tf.callable([0, 0, tf.callable(1,1)], tf.undef), 156 tf.callable([0, 0, tf.callable(2,2)], tf.undef), 157 tf.callable([0, 0], tf.iterable(tf.tuple([key_type, tf.any])))), 158 'cached_file_data' => tf.callable(tf.string, tf.optional(tf.callable([1, 1]))) 159 } 160 ).resolve(loader) 161 end
Public Instance Methods
Will call the given block to obtain a textual explanation if explanation support is active.
# File lib/puppet/pops/lookup/context.rb 178 def explain(&block) 179 @lookup_invocation.report_text(&block) 180 nil 181 end
Resolve interpolation expressions in the given value @param [Object] value @return [Object] the value with all interpolation expressions resolved
# File lib/puppet/pops/lookup/context.rb 186 def interpolate(value) 187 @function_context.interpolate(value, @lookup_invocation, true) 188 end
@api private
# File lib/puppet/pops/lookup/context.rb 195 def invocation 196 @lookup_invocation 197 end
# File lib/puppet/pops/lookup/context.rb 190 def not_found 191 throw :no_such_key 192 end