Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit d16ee78

Browse files
committed
Rewrite SharedContext to use a simple record/playback mechanism.
1 parent 3aad512 commit d16ee78

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

lib/rspec/core/shared_context.rb

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,33 @@ module Core
1717
# # ...
1818
# end
1919
module SharedContext
20-
include Hooks
21-
include MemoizedHelpers::ClassMethods
22-
2320
def included(group)
24-
[:before, :after].each do |type|
25-
[:all, :each].each do |scope|
26-
group.hooks[type][scope].concat hooks[type][scope]
27-
end
28-
end
29-
_nested_group_declarations.each do |name, block, *args|
30-
group.describe name, *args, &block
21+
__shared_context_recordings.each do |recording|
22+
recording.playback_onto(group)
3123
end
3224
end
3325

34-
def describe(name, *args, &block)
35-
_nested_group_declarations << [name, block, *args]
26+
def __shared_context_recordings
27+
@__shared_context_recordings ||= []
3628
end
3729

38-
alias_method :context, :describe
39-
40-
private
30+
Recording = Struct.new(:method_name, :args, :block) do
31+
def playback_onto(group)
32+
group.__send__(method_name, *args, &block)
33+
end
34+
end
4135

42-
def _nested_group_declarations
43-
@_nested_group_declarations ||= []
36+
def self.record(*methods)
37+
methods.each do |meth|
38+
class_eval <<-EOS, __FILE__, __LINE__ + 1
39+
def #{meth}(*args, &block)
40+
__shared_context_recordings << Recording.new(:#{meth}, args, block)
41+
end
42+
EOS
43+
end
4444
end
45+
46+
record :before, :after, :around, :subject, :let, :its, :describe, :context
4547
end
4648
end
4749

0 commit comments

Comments
 (0)