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
24 def initialize(initial_bindings)
25   @stack = Puppet::ThreadLocal.new(EmptyStack.new.push(initial_bindings))
26 
27   # By initializing @rollbacks to nil and creating a hash lazily when #mark or
28   # #rollback are called we ensure that the hashes are never shared between
29   # threads and it's safe to mutate them
30   @rollbacks = Puppet::ThreadLocal.new(nil)
31 end

Public Instance Methods

lookup(name, &block) click to toggle source

@api private

   # File lib/puppet/context.rb
54 def lookup(name, &block)
55   @stack.value.lookup(name, &block)
56 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
73 def mark(name)
74   @rollbacks.value ||= {}
75   if @rollbacks.value[name].nil?
76     @rollbacks.value[name] = @stack.value
77   else
78     raise DuplicateRollbackMarkError, _("Mark for '%{name}' already exists") % { name: name }
79   end
80 end
override(bindings, description = '') { || ... } click to toggle source

@api private

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

@api private

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

@api private

   # File lib/puppet/context.rb
34 def push(overrides, description = '')
35   @stack.value = @stack.value.push(overrides, description)
36 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
90 def rollback(name)
91   @rollbacks.value ||= {}
92   if @rollbacks.value[name].nil?
93     raise UnknownRollbackMarkError, _("Unknown mark '%{name}'") % { name: name }
94   end
95 
96   @stack.value = @rollbacks.value.delete(name)
97 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
42 def unsafe_push_global(overrides, description = '')
43   @stack = Puppet::ThreadLocal.new(
44     @stack.value.push(overrides, description)
45   )
46 end