Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactoring: alias prepend and append on HookCollection

- a bit more OO
- one less conditional per hook registration
  • Loading branch information...
commit 076d683e5c93858a50e6aa8ec51fb2c6291503cd 1 parent de01e05
@dchelimsky dchelimsky authored
Showing with 25 additions and 14 deletions.
  1. +25 −14 lib/rspec/core/hooks.rb
View
39 lib/rspec/core/hooks.rb
@@ -48,7 +48,16 @@ def display_name
end
end
+ module HookCollectionAliases
+ def self.included(host)
+ host.send :alias_method, :prepend, :unshift
+ host.send :alias_method, :append, :push
+ end
+ end
+
class HookCollection < Array
+ include HookCollectionAliases
+
def for(example_or_group)
self.class.new(select {|hook| hook.options_apply?(example_or_group)}).
with(example_or_group)
@@ -64,18 +73,9 @@ def run
end
end
- class GroupHookCollection < Array
- def for(group)
- @group = group
- self
- end
-
- def run
- shift.run(@group) until empty?
- end
- end
-
class AroundHookCollection < Array
+ include HookCollectionAliases
+
def for(example, initial_procsy=nil)
self.class.new(select {|hook| hook.options_apply?(example)}).
with(example, initial_procsy)
@@ -96,6 +96,17 @@ def run
end
end
+ class GroupHookCollection < Array
+ def for(group)
+ @group = group
+ self
+ end
+
+ def run
+ shift.run(@group) until empty?
+ end
+ end
+
module RegistersGlobals
def register_globals host, globals
[:before, :after, :around].each do |position|
@@ -119,8 +130,8 @@ def process host, globals, position, scope
def hooks
@hooks ||= {
:around => { :each => AroundHookCollection.new },
- :before => { :each => [], :all => [], :suite => HookCollection.new },
- :after => { :each => [], :all => [], :suite => HookCollection.new }
+ :before => { :each => HookCollection.new, :all => HookCollection.new, :suite => HookCollection.new },
+ :after => { :each => HookCollection.new, :all => HookCollection.new, :suite => HookCollection.new }
}.extend(RegistersGlobals)
end
@@ -446,7 +457,7 @@ def after_each_hooks_for(example)
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))
+ hooks[hook][scope].send(prepend_or_append, block.extend(EXTENSIONS[hook]).with(options))
end
def find_hook(hook, scope, example_or_group, initial_procsy)
Please sign in to comment.
Something went wrong with that request. Please try again.