Sha256: 3de4f8a20bf17ced221eda6fbe8835c0bfc5dfdb39e858cebbda562ccec058a8

Contents?: true

Size: 1.42 KB

Versions: 80

Compression:

Stored size: 1.42 KB

Contents

require 'vcr/util/variable_args_block_caller'

module VCR
  # @private
  module Hooks
    include VariableArgsBlockCaller

    FilteredHook = Struct.new(:hook, :filters) do
      include VariableArgsBlockCaller

      def conditionally_invoke(*args)
        filters = Array(self.filters)
        return if filters.any? { |f| !call_block(f.to_proc, *args) }
        call_block(hook, *args)
      end
    end

    def self.included(klass)
      klass.class_eval do
        extend ClassMethods
        hooks_module = Module.new
        const_set("DefinedHooks", hooks_module)
        include hooks_module
      end
    end

    def invoke_hook(hook_type, *args)
      hooks[hook_type].map do |hook|
        hook.conditionally_invoke(*args)
      end
    end

    def clear_hooks
      hooks.clear
    end

    def hooks
      @hooks ||= Hash.new do |hash, hook_type|
        hash[hook_type] = []
      end
    end

    def has_hooks_for?(hook_type)
      hooks[hook_type].any?
    end

    # @private
    module ClassMethods
      def define_hook(hook_type, prepend = false)
        placement_method = prepend ? :unshift : :<<

        # Put the hook methods in a module so we can override and super to these methods.
        self::DefinedHooks.module_eval do
          define_method hook_type do |*filters, &hook|
            hooks[hook_type].send(placement_method, FilteredHook.new(hook, filters))
          end
        end
      end
    end
  end
end

Version data entries

80 entries across 80 versions & 9 rubygems

Version Path
cloudsmith-api-2.0.15 vendor/bundle/ruby/2.6.0/gems/vcr-3.0.3/lib/vcr/util/hooks.rb
cloudsmith-api-2.0.14 vendor/bundle/ruby/2.6.0/gems/vcr-3.0.3/lib/vcr/util/hooks.rb
cloudsmith-api-2.0.13 vendor/bundle/ruby/2.6.0/gems/vcr-3.0.3/lib/vcr/util/hooks.rb
cloudsmith-api-2.0.12 vendor/bundle/ruby/2.6.0/gems/vcr-3.0.3/lib/vcr/util/hooks.rb
cloudsmith-api-2.0.11 vendor/bundle/ruby/2.6.0/gems/vcr-3.0.3/lib/vcr/util/hooks.rb
cloudsmith-api-2.0.10 vendor/bundle/ruby/2.6.0/gems/vcr-3.0.3/lib/vcr/util/hooks.rb
cloudsmith-api-2.0.9 vendor/bundle/ruby/2.6.0/gems/vcr-3.0.3/lib/vcr/util/hooks.rb
cloudsmith-api-2.0.8 vendor/bundle/ruby/2.6.0/gems/vcr-3.0.3/lib/vcr/util/hooks.rb
cloudsmith-api-2.0.7 vendor/bundle/ruby/2.6.0/gems/vcr-3.0.3/lib/vcr/util/hooks.rb
cloudsmith-api-2.0.6 vendor/bundle/ruby/2.6.0/gems/vcr-3.0.3/lib/vcr/util/hooks.rb
cloudsmith-api-2.0.5 vendor/bundle/ruby/2.6.0/gems/vcr-3.0.3/lib/vcr/util/hooks.rb
cloudsmith-api-2.0.4 vendor/bundle/ruby/2.6.0/gems/vcr-3.0.3/lib/vcr/util/hooks.rb
cloudsmith-api-2.0.3 vendor/bundle/ruby/2.6.0/gems/vcr-3.0.3/lib/vcr/util/hooks.rb
cloudsmith-api-2.0.2 vendor/bundle/ruby/2.6.0/gems/vcr-3.0.3/lib/vcr/util/hooks.rb
cloudsmith-api-2.0.1 vendor/bundle/ruby/2.6.0/gems/vcr-3.0.3/lib/vcr/util/hooks.rb
cloudsmith-api-2.0.0 vendor/bundle/ruby/2.6.0/gems/vcr-3.0.3/lib/vcr/util/hooks.rb
cloudsmith-api-1.142.3 vendor/bundle/ruby/2.6.0/gems/vcr-3.0.3/lib/vcr/util/hooks.rb
cloudsmith-api-1.120.3 vendor/bundle/ruby/2.6.0/gems/vcr-3.0.3/lib/vcr/util/hooks.rb
cloudsmith-api-1.61.3 vendor/bundle/ruby/2.6.0/gems/vcr-3.0.3/lib/vcr/util/hooks.rb
cloudsmith-api-1.42.3 vendor/bundle/ruby/2.6.0/gems/vcr-3.0.3/lib/vcr/util/hooks.rb