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, 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.



21
22
23
24
25
26
27
28
# File 'lib/puppet/context.rb', line 21

def initialize(initial_bindings)
  @table = initial_bindings
  @ignores = []
  @description = "root"
  @id = 0
  @rollbacks = {}
  @stack = [[0, nil, nil]]
end

Instance Method Details

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



71
72
73
# File 'lib/puppet/context.rb', line 71

def ignore(name)
  @ignores << name
end

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



48
49
50
51
52
53
54
55
56
57
# File 'lib/puppet/context.rb', line 48

def lookup(name, &block)
  if @table.include?(name) && !@ignores.include?(name)
    value = @table[name]
    value.is_a?(Proc) ? (@table[name] = value.call) : value
  elsif block
    block.call
  else
    raise UndefinedBindingError, _("no '%{name}' in %{table} at top of %{stack}") % { name: name, table: @table.inspect, stack: @stack.inspect }
  end
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



89
90
91
92
93
94
95
# File 'lib/puppet/context.rb', line 89

def mark(name)
  if @rollbacks[name].nil?
    @rollbacks[name] = @stack[-1][0]
  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
68
# File 'lib/puppet/context.rb', line 60

def override(bindings, description = "", &block)
  mark_point = "override over #{@stack[-1][0]}"
  mark(mark_point)
  push(bindings, description)

  yield
ensure
  rollback(mark_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.



39
40
41
42
43
44
45
# File 'lib/puppet/context.rb', line 39

def pop
  if @stack[-1][0] == 0
    raise(StackUnderflow, _("Attempted to pop, but already at root of the context stack."))
  else
    (_, @table, @description) = @stack.pop
  end
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.



31
32
33
34
35
36
# File 'lib/puppet/context.rb', line 31

def push(overrides, description = "")
  @id += 1
  @stack.push([@id, @table, @description])
  @table = @table.merge(overrides || {})
  @description = description
end

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



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

def restore(name)
  if @ignores.include?(name)
    @ignores.delete(name)
  else
    raise UndefinedBindingError, _("no '%{name}' in ignores %{ignores} at top of %{stack}") % { name: name, ignores: @ignores.inspect, stack: @stack.inspect }
  end
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



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/puppet/context.rb', line 105

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

  while @stack[-1][0] != @rollbacks[name]
    pop
  end

  @rollbacks.delete(name)
end