contextr 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -18
- data/License.txt +20 -0
- data/Manifest.txt +15 -19
- data/README.txt +2 -49
- data/Rakefile +55 -88
- data/examples/general.rb +152 -0
- data/examples/ordering.rb +29 -0
- data/lib/contextr.rb +15 -15
- data/lib/contextr/class_methods.rb +90 -0
- data/lib/contextr/core_ext.rb +2 -0
- data/lib/contextr/core_ext/module.rb +18 -0
- data/lib/contextr/core_ext/object.rb +9 -0
- data/lib/contextr/event_machine.rb +71 -0
- data/lib/contextr/layer.rb +57 -381
- data/lib/contextr/modules/mutex_code.rb +22 -0
- data/lib/contextr/modules/unique_id.rb +13 -0
- data/lib/contextr/public_api.rb +58 -0
- data/lib/contextr/version.rb +2 -2
- data/lib/ext/active_support_subset.rb +85 -0
- data/spec/contextr_spec.rb +11 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +1 -1
- data/test/test_contextr.rb +11 -0
- data/website/index.html +23 -101
- data/website/index.txt +27 -95
- data/website/stylesheets/screen.css +9 -0
- data/website/template.rhtml +3 -8
- metadata +21 -26
- data/COPYING.txt +0 -340
- data/LICENSE.txt +0 -57
- data/examples/education.rb +0 -71
- data/examples/fibonacci.rb +0 -124
- data/examples/with_current_context.rb +0 -29
- data/lib/contextr/contextr.rb +0 -160
- data/lib/core_ext/class.rb +0 -29
- data/lib/core_ext/module.rb +0 -62
- data/lib/core_ext/proc.rb +0 -53
- data/lib/ext/method_nature.rb +0 -80
- data/rake/group_spec_task.rb +0 -7
- data/rake/specific_group_spec_task.rb +0 -7
- data/spec/contextr/contextr_api_spec.rb +0 -126
- data/spec/contextr/contextr_class_side_spec.rb +0 -77
- data/spec/contextr/contextr_functional_spec.rb +0 -293
- data/spec/core_ext/module_spec.rb +0 -101
- data/spec/core_ext/proc_spec.rb +0 -61
- data/tasks/annotations.rb +0 -107
- data/test/contextr/test_contextr.rb +0 -7
data/lib/core_ext/module.rb
DELETED
@@ -1,62 +0,0 @@
|
|
1
|
-
class Module
|
2
|
-
# returns the name without namespace prefixes
|
3
|
-
#
|
4
|
-
# module A
|
5
|
-
# module B
|
6
|
-
# module C
|
7
|
-
# end
|
8
|
-
# end
|
9
|
-
# end
|
10
|
-
# module C
|
11
|
-
# end
|
12
|
-
#
|
13
|
-
# A::B::C.name # => "A::B::C"
|
14
|
-
# C.name # => "C"
|
15
|
-
# A::B::C.namespace_free_name # => "C"
|
16
|
-
# C.namespace_free_name # => "C"
|
17
|
-
#
|
18
|
-
def namespace_free_name
|
19
|
-
self.name.match( /(\w*?)$/ )[1]
|
20
|
-
end
|
21
|
-
|
22
|
-
# allows the definition of an attr_accessor with a setter, that is used
|
23
|
-
# to set the instance variable if it is accessed before set.
|
24
|
-
#
|
25
|
-
# class A
|
26
|
-
# attr_accessor_with_default_setter :first_access { Time.now }
|
27
|
-
# end
|
28
|
-
#
|
29
|
-
# A.new.first_access # => Wed May 09 18:23:36 0200 2007
|
30
|
-
#
|
31
|
-
# a1 = A.new
|
32
|
-
# a1.first_access # => Wed May 09 18:23:38 0200 2007
|
33
|
-
# a1.first_access # => Wed May 09 18:23:38 0200 2007
|
34
|
-
#
|
35
|
-
# a2 = A.new
|
36
|
-
# a2.first_access = Time.now - 10.days
|
37
|
-
# a2.first_access # => Sun Apr 29 18:23:40 0200 2007
|
38
|
-
#
|
39
|
-
# :call-seq:
|
40
|
-
# attr_accessor_with_default_setter(symbol, ...) { ... }
|
41
|
-
#
|
42
|
-
def attr_accessor_with_default_setter( *syms )
|
43
|
-
raise 'Default value in block required' unless block_given?
|
44
|
-
syms.each do | sym |
|
45
|
-
module_eval do
|
46
|
-
attr_writer( sym )
|
47
|
-
define_method( sym ) do | |
|
48
|
-
class << self; self; end.class_eval do
|
49
|
-
attr_reader( sym )
|
50
|
-
end
|
51
|
-
|
52
|
-
if instance_variables.include? "@#{sym}"
|
53
|
-
instance_variable_get( "@#{sym}" )
|
54
|
-
else
|
55
|
-
instance_variable_set( "@#{sym}", yield )
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
nil
|
61
|
-
end
|
62
|
-
end
|
data/lib/core_ext/proc.rb
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
class Proc
|
2
|
-
|
3
|
-
# Returns the Proc converted to an UnboundMethod within the the given Class
|
4
|
-
#
|
5
|
-
# class A
|
6
|
-
# def a
|
7
|
-
# "a"
|
8
|
-
# end
|
9
|
-
# end
|
10
|
-
#
|
11
|
-
# um_a = A.instance_method( :a ) # => #<UnboundMethod: A#a>
|
12
|
-
# um_a.bind( A.new ).call # => "a"
|
13
|
-
#
|
14
|
-
# b = lambda do "b" end
|
15
|
-
# um_b = b.to_unbound_method( A ) # => #<UnboundMethod: A#_um_from_proc>
|
16
|
-
# um_b.bind( A.new ).call # => "b"
|
17
|
-
#
|
18
|
-
def to_unbound_method( klass )
|
19
|
-
raise ArgumentError.new( "Only class objects allowed in parameter list"
|
20
|
-
) unless klass.kind_of?( Class )
|
21
|
-
|
22
|
-
proc_object = self
|
23
|
-
klass.class_eval do
|
24
|
-
define_method( :_um_from_proc, &proc_object )
|
25
|
-
end
|
26
|
-
|
27
|
-
unbound_method = klass.instance_method( :_um_from_proc )
|
28
|
-
|
29
|
-
klass.class_eval do
|
30
|
-
undef_method( :_um_from_proc )
|
31
|
-
end
|
32
|
-
|
33
|
-
unbound_method
|
34
|
-
end
|
35
|
-
|
36
|
-
# joins to blocks into a new one, that forwards on excution the given
|
37
|
-
# arguments.
|
38
|
-
#
|
39
|
-
# a = lambda do print "a" end
|
40
|
-
# b = lambda do print "b" end
|
41
|
-
#
|
42
|
-
# ab = a + b
|
43
|
-
#
|
44
|
-
# ab.call # => "ab"
|
45
|
-
#
|
46
|
-
def +( other_proc )
|
47
|
-
this_proc = self
|
48
|
-
lambda do | *arguments |
|
49
|
-
this_proc.call( *arguments )
|
50
|
-
other_proc.call( *arguments )
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
data/lib/ext/method_nature.rb
DELETED
@@ -1,80 +0,0 @@
|
|
1
|
-
# This class was created by Christian Neukirchen in the context of
|
2
|
-
# EuRuKo 2005 and is licensed under the same terms as Ruby.
|
3
|
-
#
|
4
|
-
# It is used within ContextR to provide an interface to manipulate the calling
|
5
|
-
# context within a method wrapper.
|
6
|
-
#
|
7
|
-
# For more information see the corresponding slides at
|
8
|
-
# http://chneukirchen.org/talks/euruko-2005/chneukirchen-euruko2005-contextr.pdf
|
9
|
-
#
|
10
|
-
# (c) 2005 - Christian Neukirchen - http://chneukirchen.org
|
11
|
-
#
|
12
|
-
# ---
|
13
|
-
#
|
14
|
-
# It provides access
|
15
|
-
# [+arguments+] to read and manipulate method's arguments.
|
16
|
-
# [+return_value+] to read and manipulate the method's return value
|
17
|
-
#
|
18
|
-
class MethodNature < Struct.new(:arguments, :return_value, :break, :block)
|
19
|
-
|
20
|
-
# stops the execution of the following method wrappers and potentially the
|
21
|
-
# core method.
|
22
|
-
#
|
23
|
-
# class A
|
24
|
-
# def a
|
25
|
-
# "a"
|
26
|
-
# end
|
27
|
-
#
|
28
|
-
# layer :foo
|
29
|
-
#
|
30
|
-
# foo.pre :a do | method_nature |
|
31
|
-
# method_nature.break! "b"
|
32
|
-
# end
|
33
|
-
# end
|
34
|
-
#
|
35
|
-
# A.new.a # => "a"
|
36
|
-
# ContextR::with_layers :foo do
|
37
|
-
# A.new.a # => "b"
|
38
|
-
# end
|
39
|
-
#
|
40
|
-
# If it is called without parameter, the return value will not be changed.
|
41
|
-
#
|
42
|
-
# :call-seq:
|
43
|
-
# break!
|
44
|
-
# break!( return_value )
|
45
|
-
#
|
46
|
-
def break!( *value )
|
47
|
-
self.break = true
|
48
|
-
self.return_value = value.first unless value.empty?
|
49
|
-
end
|
50
|
-
|
51
|
-
# calls the next wrapper with an around method. It corresponds to a super call
|
52
|
-
# in an inheritance hierarchy.
|
53
|
-
#
|
54
|
-
# The example attaches to each method in the class A an around wrapper which
|
55
|
-
# logs access.
|
56
|
-
#
|
57
|
-
# class A
|
58
|
-
# def a
|
59
|
-
# "a"
|
60
|
-
# end
|
61
|
-
#
|
62
|
-
# layer :log
|
63
|
-
#
|
64
|
-
# instance_methods.each do | method |
|
65
|
-
#
|
66
|
-
# log.around method.to_sym do | method_nature |
|
67
|
-
# logger.info "before #{self.class}##{method}"
|
68
|
-
#
|
69
|
-
# method_nature.call_next
|
70
|
-
#
|
71
|
-
# logger.info "after #{self.class}##{method}"
|
72
|
-
# end
|
73
|
-
# end
|
74
|
-
#
|
75
|
-
# end
|
76
|
-
#
|
77
|
-
def call_next
|
78
|
-
block.call( *arguments )
|
79
|
-
end
|
80
|
-
end
|
data/rake/group_spec_task.rb
DELETED
@@ -1,126 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
-
|
3
|
-
class ContextRApiFoo
|
4
|
-
def non_contextified_method
|
5
|
-
"non_contextified_method"
|
6
|
-
end
|
7
|
-
def foo
|
8
|
-
"foo"
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
describe 'Each class' do
|
13
|
-
it 'should provide a method to enable a single layer' do
|
14
|
-
lambda do
|
15
|
-
class ContextRApiFoo
|
16
|
-
layer :bar
|
17
|
-
end
|
18
|
-
end.should_not raise_error
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'should provide a method to access these layers by name' do
|
22
|
-
begin
|
23
|
-
class ContextRApiFoo
|
24
|
-
bar
|
25
|
-
end
|
26
|
-
end.layer.should == ContextR::BarLayer
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
describe 'Each layer in a class' do
|
31
|
-
it 'should allow the definition of pre method wrappers ' +
|
32
|
-
'with `pre`' do
|
33
|
-
lambda do
|
34
|
-
class ContextRApiFoo
|
35
|
-
bar.pre :foo do
|
36
|
-
@pre_visited = true
|
37
|
-
@pre_count = ( @pre_count || 0 ) + 1
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end.should_not raise_error
|
41
|
-
end
|
42
|
-
it 'should allow the definition of post method wrappers ' +
|
43
|
-
'with `post`' do
|
44
|
-
lambda do
|
45
|
-
class ContextRApiFoo
|
46
|
-
bar.post :foo do
|
47
|
-
@post_visited = true
|
48
|
-
@post_count = ( @post_count || 0 ) + 1
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end.should_not raise_error
|
52
|
-
end
|
53
|
-
it 'should allow the definition of around method wrappers ' +
|
54
|
-
'with `around`' do
|
55
|
-
lambda do
|
56
|
-
class ContextRApiFoo
|
57
|
-
bar.around :foo do | method_nature |
|
58
|
-
@around_visited = true
|
59
|
-
@around_count = ( @around_count || 0 ) + 1
|
60
|
-
method_nature.call_next
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end.should_not raise_error
|
64
|
-
end
|
65
|
-
it 'should allow the definition of around method wrappers ' +
|
66
|
-
'with `wrap`' do
|
67
|
-
lambda do
|
68
|
-
class ContextRApiFoo
|
69
|
-
bar.wrap :foo do | method_nature |
|
70
|
-
@around_visited = true
|
71
|
-
@around_count = ( @around_count || 0 ) + 1
|
72
|
-
method_nature.call_next
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end.should_not raise_error
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
describe ContextR do
|
80
|
-
it "should activate layers with activate_layers" do
|
81
|
-
ContextR::activate_layers :foo
|
82
|
-
ContextR::current_layers.should include(:foo)
|
83
|
-
end
|
84
|
-
|
85
|
-
it "should activate multiple layers wiht activate_layers" do
|
86
|
-
ContextR::activate_layers :bar, :baz
|
87
|
-
ContextR::current_layers.should include(:foo)
|
88
|
-
ContextR::current_layers.should include(:bar)
|
89
|
-
ContextR::current_layers.should include(:baz)
|
90
|
-
end
|
91
|
-
|
92
|
-
it "should deactivate layers with activate_layers" do
|
93
|
-
ContextR::deactivate_layers :bar
|
94
|
-
ContextR::current_layers.should_not include(:bar)
|
95
|
-
ContextR::current_layers.should include(:foo)
|
96
|
-
ContextR::current_layers.should include(:baz)
|
97
|
-
end
|
98
|
-
|
99
|
-
it "should deactivate multiple layers wiht activate_layers" do
|
100
|
-
ContextR::deactivate_layers :foo, :baz
|
101
|
-
ContextR::current_layers.should_not include(:bar)
|
102
|
-
ContextR::current_layers.should_not include(:foo)
|
103
|
-
ContextR::current_layers.should_not include(:baz)
|
104
|
-
end
|
105
|
-
|
106
|
-
it "should allow the registration of context sensors" do
|
107
|
-
lambda do
|
108
|
-
ContextR::add_context_sensor do
|
109
|
-
[:foo]
|
110
|
-
end
|
111
|
-
end.should_not raise_error
|
112
|
-
end
|
113
|
-
|
114
|
-
it "should allow the use of with_current_context and use the sensors to " +
|
115
|
-
"compute it" do
|
116
|
-
ContextR::add_context_sensor do
|
117
|
-
[:bar]
|
118
|
-
end
|
119
|
-
lambda do
|
120
|
-
ContextR::with_current_context do
|
121
|
-
ContextR::current_layers.should include(:foo)
|
122
|
-
ContextR::current_layers.should include(:bar)
|
123
|
-
end
|
124
|
-
end.should_not raise_error
|
125
|
-
end
|
126
|
-
end
|
@@ -1,77 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + "/../spec_helper.rb"
|
2
|
-
|
3
|
-
class ContextRClassSide
|
4
|
-
class << self
|
5
|
-
def non_contextified_method
|
6
|
-
"non_contextified_method"
|
7
|
-
end
|
8
|
-
def pre_wrapped_method
|
9
|
-
"pre_wrapped_method"
|
10
|
-
end
|
11
|
-
def post_wrapped_method
|
12
|
-
"post_wrapped_method"
|
13
|
-
end
|
14
|
-
def around_wrapped_method
|
15
|
-
"around_wrapped_method"
|
16
|
-
end
|
17
|
-
|
18
|
-
layer :simple_wrappers, :dummy
|
19
|
-
|
20
|
-
simple_wrappers.pre :pre_wrapped_method do
|
21
|
-
@pre_wrapped_method_called = true
|
22
|
-
end
|
23
|
-
simple_wrappers.post :post_wrapped_method do
|
24
|
-
@post_wrapped_method_called = true
|
25
|
-
end
|
26
|
-
simple_wrappers.around :around_wrapped_method do | n |
|
27
|
-
@around_wrapped_method_called = true
|
28
|
-
n.call_next
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
describe "A contextified class" do
|
34
|
-
it "should run a simple method " +
|
35
|
-
"*normally* when all layers are deactivated" do
|
36
|
-
ContextRClassSide.non_contextified_method.should ==
|
37
|
-
"non_contextified_method"
|
38
|
-
end
|
39
|
-
|
40
|
-
it "should run a simple method " +
|
41
|
-
"*normally* when any layer is activated" do
|
42
|
-
ContextR.with_layers( :simple_wrappers ) do
|
43
|
-
ContextRClassSide.non_contextified_method.should ==
|
44
|
-
"non_contextified_method"
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
%w{pre post around}.each do | qualifier |
|
49
|
-
it "should run a #{qualifier}-ed method " +
|
50
|
-
"*normally* when all layers are deactivated" do
|
51
|
-
ContextRClassSide.send( "#{qualifier}_wrapped_method" ).should ==
|
52
|
-
"#{qualifier}_wrapped_method"
|
53
|
-
ContextRClassSide.instance_variables.should_not include(
|
54
|
-
"@#{qualifier}_wrapped_method_called" )
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should run a #{qualifier}-ed method " +
|
58
|
-
"*normally* when any layer is activated" do
|
59
|
-
ContextR.with_layers( :dummy ) do
|
60
|
-
ContextRClassSide.send( "#{qualifier}_wrapped_method" ).should ==
|
61
|
-
"#{qualifier}_wrapped_method"
|
62
|
-
ContextRClassSide.instance_variables.should_not include(
|
63
|
-
"@#{qualifier}_wrapped_method_called" )
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
it "should run a #{qualifier}-ed method with " +
|
68
|
-
"additional behaviour when a specific layer is activated" do
|
69
|
-
ContextR.with_layers( :simple_wrappers ) do
|
70
|
-
ContextRClassSide.send( "#{qualifier}_wrapped_method" ).should ==
|
71
|
-
"#{qualifier}_wrapped_method"
|
72
|
-
ContextRClassSide.instance_variables.should include(
|
73
|
-
"@#{qualifier}_wrapped_method_called" )
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
@@ -1,293 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
-
|
3
|
-
class SimpleWrapperClass
|
4
|
-
def non_contextified_method
|
5
|
-
"non_contextified_method"
|
6
|
-
end
|
7
|
-
def pre_wrapped_method
|
8
|
-
"pre_wrapped_method"
|
9
|
-
end
|
10
|
-
def post_wrapped_method
|
11
|
-
"post_wrapped_method"
|
12
|
-
end
|
13
|
-
def around_wrapped_method
|
14
|
-
"around_wrapped_method"
|
15
|
-
end
|
16
|
-
|
17
|
-
layer :simple_wrappers, :dummy
|
18
|
-
|
19
|
-
simple_wrappers.pre :pre_wrapped_method do
|
20
|
-
@pre_wrapped_method_called = true
|
21
|
-
end
|
22
|
-
simple_wrappers.post :post_wrapped_method do
|
23
|
-
@post_wrapped_method_called = true
|
24
|
-
end
|
25
|
-
simple_wrappers.around :around_wrapped_method do | n |
|
26
|
-
@around_wrapped_method_called = true
|
27
|
-
n.call_next
|
28
|
-
end
|
29
|
-
end
|
30
|
-
describe "An instance of a contextified class" do
|
31
|
-
before do
|
32
|
-
@instance = SimpleWrapperClass.new
|
33
|
-
end
|
34
|
-
|
35
|
-
it "should run a simple method " +
|
36
|
-
"*normally* when all layers are deactivated" do
|
37
|
-
@instance.non_contextified_method.should == "non_contextified_method"
|
38
|
-
end
|
39
|
-
|
40
|
-
it "should run a simple method " +
|
41
|
-
"*normally* when any layer is activated" do
|
42
|
-
ContextR.with_layers( :simple_wrappers ) do
|
43
|
-
@instance.non_contextified_method.should == "non_contextified_method"
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
%w{pre post around}.each do | qualifier |
|
48
|
-
it "should run a #{qualifier}-ed method " +
|
49
|
-
"*normally* when all layers are deactivated" do
|
50
|
-
@instance.send( "#{qualifier}_wrapped_method" ).should ==
|
51
|
-
"#{qualifier}_wrapped_method"
|
52
|
-
@instance.instance_variables.should_not include(
|
53
|
-
"@#{qualifier}_wrapped_method_called" )
|
54
|
-
end
|
55
|
-
|
56
|
-
it "should run a #{qualifier}-ed method " +
|
57
|
-
"*normally* when any layer is activated" do
|
58
|
-
ContextR.with_layers( :dummy ) do
|
59
|
-
@instance.send( "#{qualifier}_wrapped_method" ).should ==
|
60
|
-
"#{qualifier}_wrapped_method"
|
61
|
-
@instance.instance_variables.should_not include(
|
62
|
-
"@#{qualifier}_wrapped_method_called" )
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
it "should run a #{qualifier}-ed method with " +
|
67
|
-
"additional behaviour when a specific layer is activated" do
|
68
|
-
ContextR.with_layers( :simple_wrappers ) do
|
69
|
-
@instance.send( "#{qualifier}_wrapped_method" ).should ==
|
70
|
-
"#{qualifier}_wrapped_method"
|
71
|
-
@instance.instance_variables.should include(
|
72
|
-
"@#{qualifier}_wrapped_method_called" )
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
it "should run a #{qualifier}-ed method without additional " +
|
77
|
-
"behaviour when a specific layer is activated and afterwards " +
|
78
|
-
"deactivated" do
|
79
|
-
ContextR.with_layers( :simple_wrappers ) do
|
80
|
-
ContextR.without_layers( :simple_wrappers ) do
|
81
|
-
@instance.send( "#{qualifier}_wrapped_method" ).should ==
|
82
|
-
"#{qualifier}_wrapped_method"
|
83
|
-
@instance.instance_variables.should_not include(
|
84
|
-
"@#{qualifier}_wrapped_method_called" )
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
class NestedLayerActivationClass
|
92
|
-
attr_accessor :execution
|
93
|
-
def initialize
|
94
|
-
@execution = []
|
95
|
-
end
|
96
|
-
|
97
|
-
def preed_method
|
98
|
-
@execution << "core"
|
99
|
-
"preed_method"
|
100
|
-
end
|
101
|
-
|
102
|
-
def posted_method
|
103
|
-
@execution << "core"
|
104
|
-
"posted_method"
|
105
|
-
end
|
106
|
-
|
107
|
-
def arounded_method
|
108
|
-
@execution << "core"
|
109
|
-
"arounded_method"
|
110
|
-
end
|
111
|
-
|
112
|
-
layer :outer_layer, :inner_layer
|
113
|
-
|
114
|
-
inner_layer.pre :preed_method do
|
115
|
-
@execution << "inner_layer"
|
116
|
-
end
|
117
|
-
|
118
|
-
outer_layer.pre :preed_method do
|
119
|
-
@execution << "outer_layer"
|
120
|
-
end
|
121
|
-
|
122
|
-
inner_layer.post :posted_method do
|
123
|
-
@execution << "inner_layer"
|
124
|
-
end
|
125
|
-
|
126
|
-
outer_layer.post :posted_method do
|
127
|
-
@execution << "outer_layer"
|
128
|
-
end
|
129
|
-
|
130
|
-
inner_layer.around :arounded_method do | n |
|
131
|
-
@execution << "inner_layer_pre"
|
132
|
-
n.call_next
|
133
|
-
@execution << "inner_layer_post"
|
134
|
-
end
|
135
|
-
|
136
|
-
outer_layer.around :arounded_method do | n |
|
137
|
-
@execution << "outer_layer_pre"
|
138
|
-
n.call_next
|
139
|
-
@execution << "outer_layer_post"
|
140
|
-
end
|
141
|
-
|
142
|
-
def contextualized_method
|
143
|
-
@execution << "core"
|
144
|
-
"contextualized_method"
|
145
|
-
end
|
146
|
-
|
147
|
-
layer :break_in_pre, :break_in_post, :break_in_around
|
148
|
-
layer :other_layer
|
149
|
-
|
150
|
-
break_in_pre.pre :contextualized_method do | n |
|
151
|
-
n.return_value = "contextualized_method"
|
152
|
-
@execution << "breaking_pre"
|
153
|
-
n.break!
|
154
|
-
end
|
155
|
-
break_in_post.post :contextualized_method do | n |
|
156
|
-
@execution << "breaking_post"
|
157
|
-
n.break!
|
158
|
-
end
|
159
|
-
break_in_around.around :contextualized_method do | n |
|
160
|
-
@execution << "breaking_around"
|
161
|
-
n.return_value = "contextualized_method"
|
162
|
-
n.break!
|
163
|
-
n.call_next
|
164
|
-
end
|
165
|
-
|
166
|
-
other_layer.pre :contextualized_method do | n |
|
167
|
-
@execution << "other_pre"
|
168
|
-
end
|
169
|
-
other_layer.post :contextualized_method do | n |
|
170
|
-
@execution << "other_post"
|
171
|
-
end
|
172
|
-
other_layer.around :contextualized_method do | n |
|
173
|
-
@execution << "other_around"
|
174
|
-
n.call_next
|
175
|
-
end
|
176
|
-
|
177
|
-
layer :multiple_pres, :multiple_posts, :multiple_arounds
|
178
|
-
multiple_pres.pre :contextualized_method do
|
179
|
-
@execution << "first_pre"
|
180
|
-
end
|
181
|
-
multiple_pres.pre :contextualized_method do
|
182
|
-
@execution << "second_pre"
|
183
|
-
end
|
184
|
-
|
185
|
-
multiple_posts.post :contextualized_method do
|
186
|
-
@execution << "first_post"
|
187
|
-
end
|
188
|
-
multiple_posts.post :contextualized_method do
|
189
|
-
@execution << "second_post"
|
190
|
-
end
|
191
|
-
|
192
|
-
multiple_arounds.around :contextualized_method do | n |
|
193
|
-
@execution << "first_around_pre"
|
194
|
-
n.call_next
|
195
|
-
@execution << "first_around_post"
|
196
|
-
end
|
197
|
-
multiple_arounds.around :contextualized_method do | n |
|
198
|
-
@execution << "second_around_pre"
|
199
|
-
n.call_next
|
200
|
-
@execution << "second_around_post"
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
pre_spec = lambda do | instance |
|
205
|
-
instance.execution.shift.should == "outer_layer"
|
206
|
-
instance.execution.shift.should == "inner_layer"
|
207
|
-
instance.execution.shift.should == "core"
|
208
|
-
end
|
209
|
-
post_spec = lambda do | instance |
|
210
|
-
instance.execution.shift.should == "core"
|
211
|
-
instance.execution.shift.should == "inner_layer"
|
212
|
-
instance.execution.shift.should == "outer_layer"
|
213
|
-
end
|
214
|
-
around_spec = lambda do | instance |
|
215
|
-
instance.execution.shift.should == "outer_layer_pre"
|
216
|
-
instance.execution.shift.should == "inner_layer_pre"
|
217
|
-
instance.execution.shift.should == "core"
|
218
|
-
instance.execution.shift.should == "inner_layer_post"
|
219
|
-
instance.execution.shift.should == "outer_layer_post"
|
220
|
-
end
|
221
|
-
|
222
|
-
pre_break_spec = lambda do | instance |
|
223
|
-
instance.execution.shift.should == "breaking_pre"
|
224
|
-
end
|
225
|
-
post_break_spec = lambda do | instance |
|
226
|
-
instance.execution.shift.should == "other_pre"
|
227
|
-
instance.execution.shift.should == "other_around"
|
228
|
-
instance.execution.shift.should == "core"
|
229
|
-
instance.execution.shift.should == "other_post"
|
230
|
-
instance.execution.shift.should == "breaking_post"
|
231
|
-
end
|
232
|
-
around_break_spec = lambda do | instance |
|
233
|
-
instance.execution.shift.should == "other_pre"
|
234
|
-
instance.execution.shift.should == "breaking_around"
|
235
|
-
end
|
236
|
-
|
237
|
-
pre_multiple_spec = lambda do | instance |
|
238
|
-
instance.execution.shift.should == "first_pre"
|
239
|
-
instance.execution.shift.should == "second_pre"
|
240
|
-
instance.execution.shift.should == "core"
|
241
|
-
end
|
242
|
-
post_multiple_spec = lambda do | instance |
|
243
|
-
instance.execution.shift.should == "core"
|
244
|
-
instance.execution.shift.should == "second_post"
|
245
|
-
instance.execution.shift.should == "first_post"
|
246
|
-
end
|
247
|
-
around_multiple_spec = lambda do | instance |
|
248
|
-
instance.execution.shift.should == "first_around_pre"
|
249
|
-
instance.execution.shift.should == "second_around_pre"
|
250
|
-
instance.execution.shift.should == "core"
|
251
|
-
instance.execution.shift.should == "second_around_post"
|
252
|
-
instance.execution.shift.should == "first_around_post"
|
253
|
-
end
|
254
|
-
|
255
|
-
%w{pre post around}.each do | qualifier |
|
256
|
-
describe "#{qualifier.capitalize} wrappers within a method" do
|
257
|
-
before do
|
258
|
-
@instance = NestedLayerActivationClass.new
|
259
|
-
end
|
260
|
-
it "should run in the sequence of nesting, when using nested " +
|
261
|
-
"activation" do
|
262
|
-
ContextR::with_layers :outer_layer do
|
263
|
-
ContextR::with_layers :inner_layer do
|
264
|
-
@instance.send( "#{qualifier}ed_method" ).should ==
|
265
|
-
"#{qualifier}ed_method"
|
266
|
-
end
|
267
|
-
end
|
268
|
-
eval("#{qualifier}_spec").call( @instance )
|
269
|
-
end
|
270
|
-
it "should run in the sequence of nesting, when using simultaneous " +
|
271
|
-
"activation" do
|
272
|
-
ContextR::with_layers :outer_layer, :inner_layer do
|
273
|
-
@instance.send( "#{qualifier}ed_method" ).should ==
|
274
|
-
"#{qualifier}ed_method"
|
275
|
-
end
|
276
|
-
eval("#{qualifier}_spec").call( @instance )
|
277
|
-
end
|
278
|
-
|
279
|
-
it "should run in the sequence of definition within the same layer" do
|
280
|
-
ContextR::with_layers "multiple_#{qualifier}s".to_sym do
|
281
|
-
@instance.contextualized_method.should == "contextualized_method"
|
282
|
-
end
|
283
|
-
eval("#{qualifier}_multiple_spec").call( @instance )
|
284
|
-
end
|
285
|
-
|
286
|
-
it "should be able to stop the execution with `break!`" do
|
287
|
-
ContextR::with_layers "break_in_#{qualifier}".to_sym, :other_layer do
|
288
|
-
@instance.contextualized_method.should == "contextualized_method"
|
289
|
-
end
|
290
|
-
eval("#{qualifier}_break_spec").call( @instance )
|
291
|
-
end
|
292
|
-
end
|
293
|
-
end
|