Skip to content

Commit

Permalink
Extract BaseHookCollection
Browse files Browse the repository at this point in the history
Previously the HookCollection was acting as a base with the subclasses
overriding (some) methods. With this change, each subclass extends the
base class by adding new methods, not overriding existing ones. Makes it
all a bit easier to reason about.
  • Loading branch information
dchelimsky committed Oct 31, 2013
1 parent a7dcd1d commit 08d8ee9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/rspec/core/example.rb
Expand Up @@ -230,8 +230,8 @@ def instance_exec_with_rescue(context = nil, &block)
end

# @private
def instance_exec(*args, &block)
@example_group_instance.instance_exec(*args, &block)
def instance_exec(example_or_procsy, &block)
@example_group_instance.instance_exec(example_or_procsy, &block)
end

private
Expand Down
15 changes: 9 additions & 6 deletions lib/rspec/core/hooks.rb
Expand Up @@ -59,20 +59,23 @@ def display_name
end
end

class HookCollection
attr_reader :hooks
protected :hooks

class BaseHookCollection
Array.public_instance_methods(false).each do |name|
define_method(name) { |*a, &b| hooks.__send__(name, *a, &b) }
end

attr_reader :hooks
protected :hooks

alias append push
alias prepend unshift

def initialize(hooks=[])
@hooks = hooks
end
end

class HookCollection < BaseHookCollection
def for(example_or_group)
self.class.
new(hooks.select {|hook| hook.options_apply?(example_or_group)}).
Expand All @@ -89,7 +92,7 @@ def run
end
end

class AroundHookCollection < HookCollection
class AroundHookCollection < BaseHookCollection
def for(example, initial_procsy=nil)
self.class.new(hooks.select {|hook| hook.options_apply?(example)}).
with(example, initial_procsy)
Expand All @@ -110,7 +113,7 @@ def run
end
end

class GroupHookCollection < HookCollection
class GroupHookCollection < BaseHookCollection
def for(group)
@group = group
self
Expand Down

0 comments on commit 08d8ee9

Please sign in to comment.