Module: IceNine::RecursionGuard

Defined in:
lib/ice_nine/support/recursion_guard.rb

Overview

Protect against infinite recursion

Class Method Summary collapse

Class Method Details

.guard(object_id) ⇒ Object

Guard against recursively calling a block with the same object

Examples:

IceNine::RecursionGuard.guard(object_id) do
  logic_which_may_recursively_call_the_containing_method
end

Parameters:

  • object_id (Integer)

Returns:

  • (Object)


20
21
22
23
24
25
26
27
28
29
# File 'lib/ice_nine/support/recursion_guard.rb', line 20

def self.guard(object_id)
  objects = guarded_objects(caller.first)
  return if objects.include?(object_id)
  begin
    objects << object_id
    yield
  ensure
    objects.delete(object_id)
  end
end