Class: Puppet::Pops::Types::RecursionGuard Private

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pops/types/recursion_guard.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.

Keeps track of self recursion of conceptual 'this' and 'that' instances using two separate maps and a state. The class is used when tracking self recursion in two objects ('this' and 'that') simultaneously. A typical example of when this is needed is when testing if 'that' Puppet Type is assignable to 'this' Puppet Type since both types may contain self references.

All comparisons are made using the `object_id` of the instance rather than the instance itself.

Constant Summary collapse

NO_SELF_RECURSION =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

0
SELF_RECURSION_IN_THIS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

1
SELF_RECURSION_IN_THAT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

2
SELF_RECURSION_IN_BOTH =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRecursionGuard

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 RecursionGuard


19
20
21
# File 'lib/puppet/pops/types/recursion_guard.rb', line 19

def initialize
  @state = NO_SELF_RECURSION
end

Instance Attribute Details

#stateObject (readonly)

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.


12
13
14
# File 'lib/puppet/pops/types/recursion_guard.rb', line 12

def state
  @state
end

Instance Method Details

#add_that(instance) ⇒ Integer

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.

Add the given argument as 'that' and return the resulting state


90
91
92
93
94
95
# File 'lib/puppet/pops/types/recursion_guard.rb', line 90

def add_that(instance)
  if (@state & SELF_RECURSION_IN_THAT) == 0
    @state = @state | SELF_RECURSION_IN_THAT if that_put(instance)
  end
  @state
end

#add_this(instance) ⇒ Integer

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.

Add the given argument as 'this' and return the resulting state


80
81
82
83
84
85
# File 'lib/puppet/pops/types/recursion_guard.rb', line 80

def add_this(instance)
  if (@state & SELF_RECURSION_IN_THIS) == 0
    @state = @state | SELF_RECURSION_IN_THIS if this_put(instance)
  end
  @state
end

#recursive_that?(instance) ⇒ Integer

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.

Checks if recursion was detected for the given argument in the 'that' context


33
34
35
# File 'lib/puppet/pops/types/recursion_guard.rb', line 33

def recursive_that?(instance)
  instance_variable_defined?(:@recursive_that_map) && @recursive_that_map.has_key?(instance.object_id)
end

#recursive_this?(instance) ⇒ Integer

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.

Checks if recursion was detected for the given argument in the 'this' context


26
27
28
# File 'lib/puppet/pops/types/recursion_guard.rb', line 26

def recursive_this?(instance)
  instance_variable_defined?(:@recursive_this_map) && @recursive_this_map.has_key?(instance.object_id)
end

#that_countObject

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 the number of objects added to the `that` map


103
104
105
# File 'lib/puppet/pops/types/recursion_guard.rb', line 103

def that_count
  instance_variable_defined?(:@that_map) ? @that_map.size : 0
end

#this_countObject

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 the number of objects added to the `this` map


98
99
100
# File 'lib/puppet/pops/types/recursion_guard.rb', line 98

def this_count
  instance_variable_defined?(:@this_map) ? @this_map.size : 0
end

#with_that(instance) {|@state| ... } ⇒ 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.

Add the given argument as 'that' invoke the given block with the resulting state

Yields:


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/puppet/pops/types/recursion_guard.rb', line 60

def with_that(instance)
  if (@state & SELF_RECURSION_IN_THAT) == 0
    tc = that_count
    @state = @state | SELF_RECURSION_IN_THAT if that_put(instance)
    if tc < that_count
      # recursive state detected
      result = yield(@state)

      # pop state
      @state &= ~SELF_RECURSION_IN_THAT
      @that_map.delete(instance.object_id)
      return result
    end
  end
  yield(@state)
end

#with_this(instance) {|@state| ... } ⇒ 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.

Add the given argument as 'this' invoke the given block with the resulting state

Yields:


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/puppet/pops/types/recursion_guard.rb', line 40

def with_this(instance)
  if (@state & SELF_RECURSION_IN_THIS) == 0
    tc = this_count
    @state = @state | SELF_RECURSION_IN_THIS if this_put(instance)
    if tc < this_count
      # recursive state detected
      result = yield(@state)

      # pop state
      @state &= ~SELF_RECURSION_IN_THIS
      @this_map.delete(instance.object_id)
      return result
    end
  end
  yield(@state)
end