Permalink
Browse files

minor refactoring: reduce duplication in hooks module

  • Loading branch information...
dchelimsky committed May 24, 2012
1 parent 5090cc3 commit 68379109b5157fc3115a07635b6b83f8e6d4ee5b
Showing with 18 additions and 12 deletions.
  1. +18 −12 lib/rspec/core/hooks.rb
View
@@ -263,8 +263,7 @@ def hooks
# end
# end
def before(*args, &block)
- scope, options = scope_and_options_from(*args)
- hooks[:before][scope] << block.extend(BeforeHookExtension).with(options)
+ register_hook :append, :before, *args, &block
end
alias_method :append_before, :before
@@ -274,8 +273,7 @@ def before(*args, &block)
#
# See #before for scoping semantics.
def prepend_before(*args, &block)
- scope, options = scope_and_options_from(*args)
- hooks[:before][scope].unshift block.extend(BeforeHookExtension).with(options)
+ register_hook :prepend, :before, *args, &block
end
# @api public
@@ -327,8 +325,7 @@ def prepend_before(*args, &block)
# Similarly, if more than one `after` is declared within any one scope,
# they are run in reverse order of that in which they are declared.
def after(*args, &block)
- scope, options = scope_and_options_from(*args)
- hooks[:after][scope].unshift block.extend(AfterHookExtension).with(options)
+ register_hook :prepend, :after, *args, &block
end
alias_method :prepend_after, :after
@@ -338,8 +335,7 @@ def after(*args, &block)
#
# See #after for scoping semantics.
def append_after(*args, &block)
- scope, options = scope_and_options_from(*args)
- hooks[:after][scope] << block.extend(AfterHookExtension).with(options)
+ register_hook :append, :after, *args, &block
end
# @api public
@@ -387,8 +383,7 @@ def append_after(*args, &block)
# around(:each) {|ex| FakeFS(&ex)}
#
def around(*args, &block)
- scope, options = scope_and_options_from(*args)
- hooks[:around][scope].unshift block.extend(AroundHookExtension).with(options)
+ register_hook :prepend, :around, *args, &block
end
# @private
@@ -406,6 +401,14 @@ def around_each_hooks_for(example, initial_procsy=nil)
private
+ SCOPES = [:each, :all, :suite]
+
+ EXTENSIONS = {
+ :before => BeforeHookExtension,
+ :after => AfterHookExtension,
+ :around => AroundHookExtension
+ }
+
def before_all_hooks_for(group)
GroupHookCollection.new(hooks[:before][:all]).for(group)
end
@@ -422,6 +425,11 @@ def after_each_hooks_for(example)
HookCollection.new(ancestors.map {|a| a.hooks[:after][:each]}.flatten).for(example)
end
+ def register_hook prepend_or_append, hook, *args, &block
+ scope, options = scope_and_options_from(*args)
+ hooks[hook][scope].send(prepend_or_append == :prepend ? :unshift : :push, block.extend(EXTENSIONS[hook]).with(options))
+ end
+
def find_hook(hook, scope, example_or_group, initial_procsy)
case [hook, scope]
when [:before, :all]
@@ -439,8 +447,6 @@ def find_hook(hook, scope, example_or_group, initial_procsy)
end
end
- SCOPES = [:each, :all, :suite]
-
def scope_and_options_from(*args)
return extract_scope_from(args), build_metadata_hash_from(args)
end

0 comments on commit 6837910

Please sign in to comment.