class Puppet::Context

Puppet::Context is a system for tracking services and contextual information that puppet needs to be able to run. Values are “bound” in a context when it is created and cannot be changed; however a child context can be created, using {#override}, that provides a different value.

When binding a {Proc}, the proc is called when the value is looked up, and the result is memoized for subsequent lookups. This provides a lazy mechanism that can be used to delay expensive production of values until they are needed.

@api private

Public Class Methods

new(initial_bindings) click to toggle source

@api private

   # File lib/puppet/context.rb
23 def initialize(initial_bindings)
24   @stack = Puppet::ThreadLocal.new(EmptyStack.new.push(initial_bindings))
25 
26   # By initializing @rollbacks to nil and creating a hash lazily when #mark or
27   # #rollback are called we ensure that the hashes are never shared between
28   # threads and it's safe to mutate them
29   @rollbacks = Puppet::ThreadLocal.new(nil)
30 end

Public Instance Methods

lookup(name, &block) click to toggle source

@api private

   # File lib/puppet/context.rb
53 def lookup(name, &block)
54   @stack.value.lookup(name, &block)
55 end
mark(name) click to toggle source

Mark a place on the context stack to later return to with {rollback}.

@param name [Object] The identifier for the mark

@api private

   # File lib/puppet/context.rb
72 def mark(name)
73   @rollbacks.value ||= {}
74   if @rollbacks.value[name].nil?
75     @rollbacks.value[name] = @stack.value
76   else
77     raise DuplicateRollbackMarkError, _("Mark for '%{name}' already exists") % { name: name }
78   end
79 end
override(bindings, description = '') { || ... } click to toggle source

@api private

   # File lib/puppet/context.rb
58 def override(bindings, description = '', &block)
59   saved_point = @stack.value
60   push(bindings, description)
61 
62   yield
63 ensure
64   @stack.value = saved_point
65 end
pop() click to toggle source

@api private

   # File lib/puppet/context.rb
48 def pop
49   @stack.value = @stack.value.pop
50 end
push(overrides, description = '') click to toggle source

@api private

   # File lib/puppet/context.rb
33 def push(overrides, description = '')
34   @stack.value = @stack.value.push(overrides, description)
35 end
rollback(name) click to toggle source

Roll back to a mark set by {mark}.

Rollbacks can only reach a mark accessible via {pop}. If the mark is not on the current context stack the behavior of rollback is undefined.

@param name [Object] The identifier for the mark

@api private

   # File lib/puppet/context.rb
89 def rollback(name)
90   @rollbacks.value ||= {}
91   if @rollbacks.value[name].nil?
92     raise UnknownRollbackMarkError, _("Unknown mark '%{name}'") % { name: name }
93   end
94 
95   @stack.value = @rollbacks.value.delete(name)
96 end
unsafe_push_global(overrides, description = '') click to toggle source

Push a context and make this global across threads Do not use in a context where multiple threads may already exist

@api private

   # File lib/puppet/context.rb
41 def unsafe_push_global(overrides, description = '')
42   @stack = Puppet::ThreadLocal.new(
43     @stack.value.push(overrides, description)
44   )
45 end