Sha256: 278ef8fac8ba9ac00e660ff6ea5c497fdd92155725ec617008c3d89f167263f2

Contents?: true

Size: 1.33 KB

Versions: 12

Compression:

Stored size: 1.33 KB

Contents

# encoding: utf-8

module IceNine

  # Protect against infinite recursion
  #
  # @private
  class RecursionGuard

    # Protects against infinite recursion by never yielding with the same
    # object more than once.
    class ObjectSet < self

      # Initialize a recursion guard
      #
      # @return [undefined]
      def initialize
        @object_ids = {}
      end

      # Guard against recursively calling a block with the same object
      #
      # @example
      #   recursion_guard = IceNine::RecursionGuard::ObjectSet.new
      #   recursion_guard.guard(object) do
      #     logic_which_may_be_recursively_called_with_object(recursion_guard)
      #   end
      #
      # @param [Object] object
      #
      # @return [Object]
      def guard(object)
        caller_object_id = object.__id__
        return object if @object_ids.key?(caller_object_id)
        @object_ids[caller_object_id] = nil
        yield
      end

    end # ObjectSet

    # Protects against infinite recursion by not yielding with frozen objects
    class Frozen < self

      # Guard against recursively calling a block with the same frozen object
      #
      # @param [Object] object
      #
      # @return [Object]
      def guard(object)
        return object if object.frozen?
        yield
      end

    end # Frozen

  end # RecursionGuard
end # IceNine

Version data entries

12 entries across 10 versions & 4 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/ice_nine-0.11.2/lib/ice_nine/support/recursion_guard.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/ice_nine-0.11.2/lib/ice_nine/support/recursion_guard.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/ice_nine-0.11.2/lib/ice_nine/support/recursion_guard.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/ice_nine-0.11.2/lib/ice_nine/support/recursion_guard.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/ice_nine-0.11.2/lib/ice_nine/support/recursion_guard.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/ice_nine-0.11.2/lib/ice_nine/support/recursion_guard.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/ice_nine-0.11.2/lib/ice_nine/support/recursion_guard.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/ice_nine-0.11.2/lib/ice_nine/support/recursion_guard.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/ice_nine-0.11.2/lib/ice_nine/support/recursion_guard.rb
ice_nine-0.11.2 lib/ice_nine/support/recursion_guard.rb
ice_nine-0.11.1 lib/ice_nine/support/recursion_guard.rb
ice_nine-0.11.0 lib/ice_nine/support/recursion_guard.rb