Class: Puppet::Context Private

Inherits:
Object show all
Defined in:
lib/puppet/context.rb

Overview

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.

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.

Defined Under Namespace

Classes: DuplicateRollbackMarkError, EmptyStack, Stack, StackUnderflow, TrustedInformation, UndefinedBindingError, UnknownRollbackMarkError

Instance Method Summary collapse

Constructor Details

#initialize(initial_bindings) ⇒ Context

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 a new instance of Context.



25
26
27
28
29
30
31
32
# File 'lib/puppet/context.rb', line 25

def initialize(initial_bindings)
  @stack = Puppet::ThreadLocal.new(EmptyStack.new.push(initial_bindings))

  # By initializing @rollbacks to nil and creating a hash lazily when #mark or
  # #rollback are called we ensure that the hashes are never shared between
  # threads and it's safe to mutate them
  @rollbacks = Puppet::ThreadLocal.new(nil)
end

Instance Method Details

#lookup(name, &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.



55
56
57
# File 'lib/puppet/context.rb', line 55

def lookup(name, &block)
  @stack.value.lookup(name, &block)
end

#mark(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.

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

Parameters:

  • name (Object)

    The identifier for the mark



74
75
76
77
78
79
80
81
# File 'lib/puppet/context.rb', line 74

def mark(name)
  @rollbacks.value ||= {}
  if @rollbacks.value[name].nil?
    @rollbacks.value[name] = @stack.value
  else
    raise DuplicateRollbackMarkError, _("Mark for '%{name}' already exists") % { name: name }
  end
end

#override(bindings, description = '', &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.



60
61
62
63
64
65
66
67
# File 'lib/puppet/context.rb', line 60

def override(bindings, description = '', &block)
  saved_point = @stack.value
  push(bindings, description)

  yield
ensure
  @stack.value = saved_point
end

#popObject

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.



50
51
52
# File 'lib/puppet/context.rb', line 50

def pop
  @stack.value = @stack.value.pop
end

#push(overrides, description = '') ⇒ 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.



35
36
37
# File 'lib/puppet/context.rb', line 35

def push(overrides, description = '')
  @stack.value = @stack.value.push(overrides, description)
end

#rollback(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.

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.

Parameters:

  • name (Object)

    The identifier for the mark



91
92
93
94
95
96
97
98
# File 'lib/puppet/context.rb', line 91

def rollback(name)
  @rollbacks.value ||= {}
  if @rollbacks.value[name].nil?
    raise UnknownRollbackMarkError, _("Unknown mark '%{name}'") % { name: name }
  end

  @stack.value = @rollbacks.value.delete(name)
end

#unsafe_push_global(overrides, description = '') ⇒ 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.

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



43
44
45
46
47
# File 'lib/puppet/context.rb', line 43

def unsafe_push_global(overrides, description = '')
  @stack = Puppet::ThreadLocal.new(
    @stack.value.push(overrides, description)
  )
end