module SuperDiff::RecursionGuard

Constants

PLACEHOLDER
RECURSION_GUARD_KEY

Public Class Methods

already_seen?(object) click to toggle source
# File lib/super_diff/recursion_guard.rb, line 41
def self.already_seen?(object)
  already_seen_object_ids.include?(object.object_id)
end
already_seen_object_ids() click to toggle source
# File lib/super_diff/recursion_guard.rb, line 45
def self.already_seen_object_ids
  Thread.current[RECURSION_GUARD_KEY] ||= Set.new
end
guarding_recursion_of(*objects, &block) click to toggle source
# File lib/super_diff/recursion_guard.rb, line 8
def self.guarding_recursion_of(*objects, &block)
  already_seen_objects, first_seen_objects = objects.partition do |object|
    !SuperDiff.primitive?(object) && already_seen?(object)
  end

  first_seen_objects.each do |object|
    already_seen_object_ids.add(object.object_id)
  end

  result =
    if block.arity > 0
      block.call(already_seen_objects.any?)
    else
      block.call
    end

  first_seen_objects.each do |object|
    already_seen_object_ids.delete(object.object_id)
  end

  result
end
substituting_recursion_of(*objects) { || ... } click to toggle source
# File lib/super_diff/recursion_guard.rb, line 31
def self.substituting_recursion_of(*objects)
  guarding_recursion_of(*objects) do |already_seen|
    if already_seen
      PLACEHOLDER
    else
      yield
    end
  end
end