Sha256: 1602dda33ba0d2a0b7c7585082b923b2f8ca162e446ba40a34a74c64798c3838

Contents?: true

Size: 998 Bytes

Versions: 11

Compression:

Stored size: 998 Bytes

Contents

module VCR
  module Hooks
    include VariableArgsBlockCaller

    def invoke_hook(hook, tag, *args)
      hooks_for(hook, tag).each do |callback|
        call_block(callback, *args)
      end
    end

    def clear_hooks
      hooks.clear
    end

    private

      def hooks
        @hooks ||= Hash.new do |hook_type_hash, hook_type|
          hook_type_hash[hook_type] = Hash.new do |tag_hash, tag|
            tag_hash[tag] = []
          end
        end
      end

      def define_hook(hook)
        # We must use string eval so that the dynamically
        # defined method can accept a block.
        instance_eval <<-RUBY, __FILE__, __LINE__ + 1
          def #{hook}(tag = nil, &block)
            hooks[#{hook.inspect}][tag] << block
          end
        RUBY
      end

      def hooks_for(hook, tag)
        for_hook = hooks[hook]
        hooks = for_hook[tag] # matching tagged hooks
        hooks += for_hook[nil] unless tag.nil? # untagged hooks
        hooks
      end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
vcr-1.11.3 lib/vcr/util/hooks.rb
vcr-1.11.2 lib/vcr/util/hooks.rb
vcr-1.11.1 lib/vcr/util/hooks.rb
vcr-1.10.3 lib/vcr/util/hooks.rb
vcr-1.10.2 lib/vcr/util/hooks.rb
vcr-1.10.0 lib/vcr/util/hooks.rb
vcr-1.9.0 lib/vcr/util/hooks.rb
vcr-1.8.0 lib/vcr/util/hooks.rb
vcr-1.7.2 lib/vcr/util/hooks.rb
vcr-1.7.1 lib/vcr/util/hooks.rb
vcr-1.7.0 lib/vcr/util/hooks.rb