Sha256: 8ecc500ff573a60ccf0a46c18f646b5450181ca83200671748f9b3ef04274233
Contents?: true
Size: 1.37 KB
Versions: 1
Compression:
Stored size: 1.37 KB
Contents
require 'vcr/util/variable_args_block_caller' module VCR module Hooks include VariableArgsBlockCaller def self.included(klass) klass.extend(ClassMethods) end def invoke_hook(hook, *args) invoke_tagged_hook(hook, nil, *args) end def invoke_tagged_hook(hook, tag, *args) hooks_for(hook, tag).map 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 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 module ClassMethods def define_hook(hook, prepend = false) placement_method = prepend ? :unshift : :<< # We use splat args here because 1.8.7 doesn't allow default # values for block arguments, so we have to fake it. define_method hook do |*args, &block| if args.size > 1 raise ArgumentError.new("wrong number of arguments (#{args.size} for 1)") end tag = args.first hooks[hook][tag].send(placement_method, block) end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
vcr-2.0.0.rc1 | lib/vcr/util/hooks.rb |