@@ -1749,7 +1749,8 @@ def before(scope=nil, *meta, &block)
1749
1749
handle_suite_hook ( scope , meta ) do
1750
1750
@before_suite_hooks << Hooks ::BeforeHook . new ( block , { } )
1751
1751
end || begin
1752
- add_hook_to_existing_groups ( :append , :before , scope , *meta , &block )
1752
+ metadata = Metadata . build_hash_from ( meta . dup )
1753
+ on_existing_matching_groups ( metadata , scope ) { |g | g . before ( scope , *meta , &block ) }
1753
1754
super ( scope , *meta , &block )
1754
1755
end
1755
1756
end
@@ -1772,7 +1773,8 @@ def prepend_before(scope=nil, *meta, &block)
1772
1773
handle_suite_hook ( scope , meta ) do
1773
1774
@before_suite_hooks . unshift Hooks ::BeforeHook . new ( block , { } )
1774
1775
end || begin
1775
- add_hook_to_existing_groups ( :prepend , :before , scope , *meta , &block )
1776
+ metadata = Metadata . build_hash_from ( meta . dup )
1777
+ on_existing_matching_groups ( metadata , scope ) { |g | g . prepend_before ( scope , *meta , &block ) }
1776
1778
super ( scope , *meta , &block )
1777
1779
end
1778
1780
end
@@ -1790,7 +1792,8 @@ def after(scope=nil, *meta, &block)
1790
1792
handle_suite_hook ( scope , meta ) do
1791
1793
@after_suite_hooks . unshift Hooks ::AfterHook . new ( block , { } )
1792
1794
end || begin
1793
- add_hook_to_existing_groups ( :prepend , :after , scope , *meta , &block )
1795
+ metadata = Metadata . build_hash_from ( meta . dup )
1796
+ on_existing_matching_groups ( metadata , scope ) { |g | g . after ( scope , *meta , &block ) }
1794
1797
super ( scope , *meta , &block )
1795
1798
end
1796
1799
end
@@ -1813,7 +1816,8 @@ def append_after(scope=nil, *meta, &block)
1813
1816
handle_suite_hook ( scope , meta ) do
1814
1817
@after_suite_hooks << Hooks ::AfterHook . new ( block , { } )
1815
1818
end || begin
1816
- add_hook_to_existing_groups ( :append , :after , scope , *meta , &block )
1819
+ metadata = Metadata . build_hash_from ( meta . dup )
1820
+ on_existing_matching_groups ( metadata , scope ) { |g | g . append_after ( scope , *meta , &block ) }
1817
1821
super ( scope , *meta , &block )
1818
1822
end
1819
1823
end
@@ -1822,7 +1826,8 @@ def append_after(scope=nil, *meta, &block)
1822
1826
#
1823
1827
# See {Hooks#around} for full `around` hook docs.
1824
1828
def around ( scope = nil , *meta , &block )
1825
- add_hook_to_existing_groups ( :prepend , :around , scope , *meta , &block )
1829
+ metadata = Metadata . build_hash_from ( meta . dup )
1830
+ on_existing_matching_groups ( metadata , scope ) { |g | g . around ( scope , *meta , &block ) }
1826
1831
super ( scope , *meta , &block )
1827
1832
end
1828
1833
@@ -2026,15 +2031,18 @@ def configure_group_with(group, module_list, application_method)
2026
2031
end
2027
2032
end
2028
2033
2029
- def on_existing_matching_groups ( meta )
2030
- world . all_example_groups . each do | group |
2031
- yield group if meta . empty? || MetadataFilter . apply? ( :any? , meta , group . metadata )
2032
- end
2033
- end
2034
+ def on_existing_matching_groups ( meta , scope = :ignore )
2035
+ groups = world . example_groups . dup
2036
+
2037
+ until groups . empty?
2038
+ group = groups . shift
2034
2039
2035
- def add_hook_to_existing_groups ( prepend_or_append , position , *meta , &block )
2036
- world . example_groups . each do |group |
2037
- group . hooks . register_global_hook ( prepend_or_append , position , *meta , &block )
2040
+ if scope == :example || scope == :each || scope . nil? ||
2041
+ meta . empty? || MetadataFilter . apply? ( :any? , meta , group . metadata )
2042
+ yield group
2043
+ else
2044
+ groups . concat ( group . children )
2045
+ end
2038
2046
end
2039
2047
end
2040
2048
0 commit comments