aspector 0.13.1 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rubocop.yml +26 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +8 -11
- data/Changelog.md +59 -0
- data/Gemfile +9 -14
- data/Gemfile.lock +84 -50
- data/README.md +118 -0
- data/Rakefile +6 -22
- data/aspector.gemspec +15 -127
- data/benchmarks/after_benchmark.rb +28 -0
- data/benchmarks/around_advice_benchmark.rb +35 -0
- data/benchmarks/around_benchmark.rb +32 -0
- data/benchmarks/before_benchmark.rb +28 -0
- data/benchmarks/benchmark_helper.rb +17 -0
- data/benchmarks/combined_benchmark.rb +36 -0
- data/benchmarks/method_invocation_benchmark.rb +30 -0
- data/benchmarks/raw_benchmark.rb +39 -0
- data/examples/activerecord_hooks.rb +10 -15
- data/examples/around_example.rb +20 -31
- data/examples/aspector_apply_example.rb +10 -17
- data/examples/aspector_example.rb +7 -16
- data/examples/cache_aspect.rb +20 -30
- data/examples/design_by_contract.rb +20 -44
- data/examples/exception_handler.rb +12 -20
- data/examples/exception_handler2.rb +16 -24
- data/examples/implicit_method_option_test.rb +8 -16
- data/examples/interception_options_example.rb +71 -0
- data/examples/logging_aspect.rb +16 -24
- data/examples/process_aspector.rb +13 -0
- data/examples/retry_aspect.rb +20 -20
- data/lib/aspector.rb +17 -15
- data/lib/aspector/advice.rb +44 -57
- data/lib/aspector/advice_metadata.rb +10 -11
- data/lib/aspector/aspect_instances.rb +2 -3
- data/lib/aspector/base.rb +6 -368
- data/lib/aspector/base_class_methods.rb +24 -55
- data/lib/aspector/deferred_logic.rb +3 -4
- data/lib/aspector/deferred_option.rb +5 -10
- data/lib/aspector/interception.rb +356 -0
- data/lib/aspector/logger.rb +18 -45
- data/lib/aspector/logging.rb +10 -29
- data/lib/aspector/method_matcher.rb +5 -6
- data/lib/aspector/object_extension.rb +4 -12
- data/lib/aspector/version.rb +3 -0
- data/spec/examples_spec.rb +59 -0
- data/spec/functionals/aspect_for_multiple_targets_spec.rb +54 -0
- data/spec/functionals/aspect_interception_options_accessing_spec.rb +112 -0
- data/spec/functionals/aspect_on_a_class_spec.rb +159 -0
- data/spec/functionals/aspect_on_an_instance_spec.rb +66 -0
- data/spec/functionals/aspector_spec.rb +138 -0
- data/spec/functionals/aspects_combined_spec.rb +37 -0
- data/spec/functionals/aspects_execution_order_spec.rb +61 -0
- data/spec/functionals/aspects_on_private_methods_spec.rb +82 -0
- data/spec/spec_helper.rb +20 -21
- data/spec/support/class_builder.rb +44 -0
- data/spec/units/advice_spec.rb +49 -0
- data/spec/units/advices/after_spec.rb +328 -0
- data/spec/units/advices/around_spec.rb +336 -0
- data/spec/units/advices/before_filter_spec.rb +287 -0
- data/spec/units/advices/before_spec.rb +237 -0
- data/spec/units/advices/raw_spec.rb +67 -0
- data/spec/units/base_class_methods_spec.rb +262 -0
- data/spec/units/base_spec.rb +133 -0
- data/spec/units/deferred_logic_spec.rb +35 -0
- data/spec/units/logger_spec.rb +20 -0
- data/spec/units/logging_spec.rb +85 -0
- data/spec/units/method_matcher_spec.rb +95 -0
- data/spec/units/object_extension_spec.rb +11 -0
- data/spec/units/special_chars_spec.rb +128 -0
- metadata +98 -246
- data/.document +0 -5
- data/.rvmrc +0 -8
- data/README.rdoc +0 -80
- data/VERSION +0 -1
- data/performance-tests/after_test.rb +0 -25
- data/performance-tests/around_advice_benchmark.rb +0 -66
- data/performance-tests/around_test.rb +0 -27
- data/performance-tests/before_test.rb +0 -25
- data/performance-tests/combined_test.rb +0 -33
- data/performance-tests/method_invocation_test.rb +0 -25
- data/performance-tests/raw_test.rb +0 -37
- data/performance-tests/test_helper.rb +0 -9
- data/run_all_examples.sh +0 -12
- data/spec/functional/advices_on_private_methods_spec.rb +0 -21
- data/spec/functional/aspect_on_eigen_class_spec.rb +0 -72
- data/spec/functional/aspect_on_object_spec.rb +0 -20
- data/spec/functional/aspector_spec.rb +0 -140
- data/spec/functional/aspects_combined_spec.rb +0 -48
- data/spec/functional/execution_order_spec.rb +0 -42
- data/spec/unit/advice_spec.rb +0 -4
- data/spec/unit/after_spec.rb +0 -88
- data/spec/unit/around_spec.rb +0 -76
- data/spec/unit/base_class_methods_spec.rb +0 -28
- data/spec/unit/base_spec.rb +0 -112
- data/spec/unit/before_spec.rb +0 -125
- data/spec/unit/deferred_logic_spec.rb +0 -23
- data/spec/unit/method_matcher_spec.rb +0 -43
- data/spec/unit/raw_spec.rb +0 -53
- data/spec/unit/special_chars_spec.rb +0 -122
data/spec/unit/base_spec.rb
DELETED
@@ -1,112 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
-
|
3
|
-
describe "Aspector::Base" do
|
4
|
-
it "default options" do
|
5
|
-
aspect = Aspector do
|
6
|
-
default :test => 'value'
|
7
|
-
end
|
8
|
-
|
9
|
-
aspect.send(:default_options)[:test].should == 'value'
|
10
|
-
end
|
11
|
-
|
12
|
-
it "#options is used to access options set when aspect is applied" do
|
13
|
-
klass = create_test_class
|
14
|
-
|
15
|
-
aspect = Aspector do
|
16
|
-
before options[:methods] do
|
17
|
-
value << "do_this"
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
aspect.apply(klass, :methods => :test)
|
22
|
-
|
23
|
-
obj = klass.new
|
24
|
-
obj.test
|
25
|
-
obj.value.should == %w"do_this test"
|
26
|
-
end
|
27
|
-
|
28
|
-
it "#apply" do
|
29
|
-
klass = create_test_class
|
30
|
-
|
31
|
-
aspect = Aspector do
|
32
|
-
before :test do value << "do_before" end
|
33
|
-
end
|
34
|
-
|
35
|
-
aspect.apply(klass)
|
36
|
-
|
37
|
-
obj = klass.new
|
38
|
-
obj.test
|
39
|
-
obj.value.should == %w"do_before test"
|
40
|
-
end
|
41
|
-
|
42
|
-
it "#apply to multiple targets at once" do
|
43
|
-
klass = create_test_class
|
44
|
-
klass2 = create_test_class
|
45
|
-
|
46
|
-
aspect = Aspector do
|
47
|
-
before :test do value << "do_before" end
|
48
|
-
end
|
49
|
-
|
50
|
-
aspect.apply(klass, klass2)
|
51
|
-
|
52
|
-
obj = klass.new
|
53
|
-
obj.test
|
54
|
-
obj.value.should == %w"do_before test"
|
55
|
-
|
56
|
-
obj2 = klass2.new
|
57
|
-
obj2.test
|
58
|
-
obj2.value.should == %w"do_before test"
|
59
|
-
end
|
60
|
-
|
61
|
-
it "use #target to add method to target class/module" do
|
62
|
-
klass = create_test_class
|
63
|
-
|
64
|
-
aspector(klass) do
|
65
|
-
target do
|
66
|
-
def do_this
|
67
|
-
value << "do_this"
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
before :test, :do_this
|
72
|
-
end
|
73
|
-
|
74
|
-
obj = klass.new
|
75
|
-
obj.test
|
76
|
-
obj.value.should == %w"do_this test"
|
77
|
-
end
|
78
|
-
|
79
|
-
it "#target takes aspect as argument" do
|
80
|
-
klass = create_test_class
|
81
|
-
|
82
|
-
class TargetArgumentTestAspect < Aspector::Base
|
83
|
-
target do |aspect|
|
84
|
-
define_method :do_this do
|
85
|
-
value << "do_this(#{aspect.class})"
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
before :test, :do_this
|
90
|
-
end
|
91
|
-
|
92
|
-
TargetArgumentTestAspect.apply(klass)
|
93
|
-
|
94
|
-
obj = klass.new
|
95
|
-
obj.test
|
96
|
-
obj.value.should == %w"do_this(TargetArgumentTestAspect) test"
|
97
|
-
end
|
98
|
-
|
99
|
-
it "should not fail if method does not exist" do
|
100
|
-
klass = Class.new
|
101
|
-
|
102
|
-
aspect = Aspector do
|
103
|
-
before options[:methods] do
|
104
|
-
# dummy advice
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
aspect.apply(klass, :methods => 'not_exist')
|
109
|
-
end
|
110
|
-
|
111
|
-
end
|
112
|
-
|
data/spec/unit/before_spec.rb
DELETED
@@ -1,125 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
-
|
3
|
-
describe "Before advices" do
|
4
|
-
it "should work" do
|
5
|
-
klass = create_test_class do
|
6
|
-
def do_this
|
7
|
-
value << "do_this"
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
aspector(klass) do
|
12
|
-
before :test, :do_this
|
13
|
-
end
|
14
|
-
|
15
|
-
obj = klass.new
|
16
|
-
obj.test
|
17
|
-
obj.value.should == %w"do_this test"
|
18
|
-
end
|
19
|
-
|
20
|
-
it "logic in block" do
|
21
|
-
klass = create_test_class
|
22
|
-
|
23
|
-
aspector(klass) do
|
24
|
-
before(:test){ value << 'do_block' }
|
25
|
-
end
|
26
|
-
|
27
|
-
obj = klass.new
|
28
|
-
obj.test
|
29
|
-
obj.value.should == %w"do_block test"
|
30
|
-
end
|
31
|
-
|
32
|
-
it "logic in String" do
|
33
|
-
klass = create_test_class
|
34
|
-
|
35
|
-
aspector(klass) do
|
36
|
-
before :test, "value << 'do_block'"
|
37
|
-
end
|
38
|
-
|
39
|
-
obj = klass.new
|
40
|
-
obj.test
|
41
|
-
obj.value.should == %w"do_block test"
|
42
|
-
end
|
43
|
-
|
44
|
-
it "new methods should work" do
|
45
|
-
klass = Class.new do
|
46
|
-
aspector do
|
47
|
-
before :test, :do_this
|
48
|
-
end
|
49
|
-
|
50
|
-
def value
|
51
|
-
@value ||= []
|
52
|
-
end
|
53
|
-
|
54
|
-
def test
|
55
|
-
value << "test"
|
56
|
-
end
|
57
|
-
|
58
|
-
def do_this
|
59
|
-
value << "do_this"
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
obj = klass.new
|
64
|
-
obj.test
|
65
|
-
obj.value.should == %w"do_this test"
|
66
|
-
end
|
67
|
-
|
68
|
-
it "method_arg" do
|
69
|
-
klass = Class.new do
|
70
|
-
aspector do
|
71
|
-
before :test, :do_this, :method_arg => true
|
72
|
-
end
|
73
|
-
|
74
|
-
def value
|
75
|
-
@value ||= []
|
76
|
-
end
|
77
|
-
|
78
|
-
def test
|
79
|
-
value << "test"
|
80
|
-
end
|
81
|
-
|
82
|
-
def do_this method
|
83
|
-
value << "do_this(#{method})"
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
obj = klass.new
|
88
|
-
obj.test
|
89
|
-
obj.value.should == %w"do_this(test) test"
|
90
|
-
end
|
91
|
-
|
92
|
-
it "implicit method option" do
|
93
|
-
klass = create_test_class do
|
94
|
-
def do_this
|
95
|
-
value << "do_this"
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
aspector klass, :method => [:test] do
|
100
|
-
before :do_this
|
101
|
-
end
|
102
|
-
|
103
|
-
o = klass.new
|
104
|
-
o.test
|
105
|
-
o.value.should == %w"do_this test"
|
106
|
-
end
|
107
|
-
|
108
|
-
it "implicit methods option" do
|
109
|
-
klass = create_test_class do
|
110
|
-
def do_this
|
111
|
-
value << "do_this"
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
aspector klass, :methods => [:test] do
|
116
|
-
before :do_this
|
117
|
-
end
|
118
|
-
|
119
|
-
o = klass.new
|
120
|
-
o.test
|
121
|
-
o.value.should == %w"do_this test"
|
122
|
-
end
|
123
|
-
|
124
|
-
end
|
125
|
-
|
@@ -1,23 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
-
|
3
|
-
module Aspector
|
4
|
-
describe DeferredLogic do
|
5
|
-
it "should work with block" do
|
6
|
-
klass = Class.new do
|
7
|
-
def self.test; 'test'; end
|
8
|
-
end
|
9
|
-
|
10
|
-
logic = DeferredLogic.new(lambda{ test })
|
11
|
-
|
12
|
-
logic.apply(klass).should == 'test'
|
13
|
-
end
|
14
|
-
|
15
|
-
it "block can take an argument" do
|
16
|
-
logic = DeferredLogic.new(lambda{|arg| arg })
|
17
|
-
|
18
|
-
logic.apply(Class.new, 'test').should == 'test'
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
@@ -1,43 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
-
|
3
|
-
module Aspector
|
4
|
-
describe MethodMatcher do
|
5
|
-
it "should match String" do
|
6
|
-
MethodMatcher.new('test').match?('test').should_not be_nil
|
7
|
-
end
|
8
|
-
|
9
|
-
it "should match regular expression" do
|
10
|
-
MethodMatcher.new(/test/).match?('test').should_not be_nil
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should return true if any item matches" do
|
14
|
-
MethodMatcher.new('test1', 'test2').match?('test2').should_not be_nil
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should return nil if none matches" do
|
18
|
-
MethodMatcher.new('test1', 'test2').match?('test3').should be_nil
|
19
|
-
end
|
20
|
-
|
21
|
-
it "deferred logic" do
|
22
|
-
logic = DeferredLogic.new ""
|
23
|
-
matcher = MethodMatcher.new(logic)
|
24
|
-
|
25
|
-
aspect = mock(Aspector::Base)
|
26
|
-
aspect.should_receive(:deferred_logic_results).with(logic).once.and_return(/test/)
|
27
|
-
|
28
|
-
matcher.match?('test', aspect).should_not be_nil
|
29
|
-
end
|
30
|
-
|
31
|
-
it "deferred option" do
|
32
|
-
option = DeferredOption.new[:methods]
|
33
|
-
matcher = MethodMatcher.new(option)
|
34
|
-
|
35
|
-
aspect = mock(Aspector::Base)
|
36
|
-
aspect.should_receive(:options).once.and_return({:methods => /test/})
|
37
|
-
|
38
|
-
matcher.match?('test', aspect).should_not be_nil
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
data/spec/unit/raw_spec.rb
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
-
|
3
|
-
describe "Raw advices" do
|
4
|
-
it "should work" do
|
5
|
-
klass = create_test_class
|
6
|
-
|
7
|
-
aspector(klass) do
|
8
|
-
raw :test do
|
9
|
-
alias test_without_aspect test
|
10
|
-
def test
|
11
|
-
value << "raw_before"
|
12
|
-
test_without_aspect
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
obj = klass.new
|
18
|
-
obj.test
|
19
|
-
obj.value.should == %w"raw_before test"
|
20
|
-
end
|
21
|
-
|
22
|
-
it "new methods should work" do
|
23
|
-
klass = Class.new do
|
24
|
-
aspector do
|
25
|
-
raw :test do
|
26
|
-
alias test_without_aspect test
|
27
|
-
def test
|
28
|
-
do_this
|
29
|
-
test_without_aspect
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def value
|
35
|
-
@value ||= []
|
36
|
-
end
|
37
|
-
|
38
|
-
def test
|
39
|
-
value << "test"
|
40
|
-
end
|
41
|
-
|
42
|
-
def do_this
|
43
|
-
value << "do_this"
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
obj = klass.new
|
48
|
-
obj.test
|
49
|
-
obj.value.should == %w"do_this test"
|
50
|
-
end
|
51
|
-
|
52
|
-
end
|
53
|
-
|
@@ -1,122 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
-
|
3
|
-
describe "special chars in method names" do
|
4
|
-
['?', '!', '='].each do |char|
|
5
|
-
it "should work with methods whose name contains #{char}" do
|
6
|
-
klass = Class.new do
|
7
|
-
aspector do
|
8
|
-
before "test#{char}", :do_this
|
9
|
-
end
|
10
|
-
|
11
|
-
def value
|
12
|
-
@value ||= []
|
13
|
-
end
|
14
|
-
|
15
|
-
def do_this *args
|
16
|
-
value << "do_this"
|
17
|
-
end
|
18
|
-
|
19
|
-
define_method "test#{char}" do |*args|
|
20
|
-
value << "test"
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
obj = klass.new
|
25
|
-
obj.send "test#{char}", 1
|
26
|
-
obj.value.should == %w"do_this test"
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
['+', '-', '*', '/', '~', '|', '%', '&', '^', '<', '>', '[]', '[]='].each do |meth|
|
31
|
-
it "should work with #{meth}" do
|
32
|
-
klass = Class.new do
|
33
|
-
aspector do
|
34
|
-
before meth, :do_this
|
35
|
-
end
|
36
|
-
|
37
|
-
def value
|
38
|
-
@value ||= []
|
39
|
-
end
|
40
|
-
|
41
|
-
def do_this *args
|
42
|
-
value << "do_this"
|
43
|
-
end
|
44
|
-
|
45
|
-
define_method meth do |*args|
|
46
|
-
value << "test"
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
obj = klass.new
|
51
|
-
obj.send meth, 1, 2
|
52
|
-
obj.value.should == %w"do_this test"
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
end
|
57
|
-
|
58
|
-
describe "special chars in class method names" do
|
59
|
-
['?', '!', '='].each do |char|
|
60
|
-
it "should work with methods whose name contains #{char}" do
|
61
|
-
Object.send :define_method, :meth_with_special_char do
|
62
|
-
"test#{char}"
|
63
|
-
end
|
64
|
-
|
65
|
-
klass = Class.new do
|
66
|
-
aspector :class_methods => true do
|
67
|
-
before meth_with_special_char, :do_this
|
68
|
-
end
|
69
|
-
|
70
|
-
class << self
|
71
|
-
def value
|
72
|
-
@value ||= []
|
73
|
-
end
|
74
|
-
|
75
|
-
def do_this *args
|
76
|
-
value << "do_this"
|
77
|
-
end
|
78
|
-
|
79
|
-
define_method meth_with_special_char do |*args|
|
80
|
-
value << "test"
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
klass.send meth_with_special_char, 1
|
86
|
-
klass.value.should == %w"do_this test"
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
['+', '-', '*', '/', '~', '|', '%', '&', '^', '<', '>', '[]', '[]='].each do |meth|
|
91
|
-
it "should work with #{meth}" do
|
92
|
-
Object.send :define_method, :meth_with_special_char do
|
93
|
-
meth
|
94
|
-
end
|
95
|
-
|
96
|
-
klass = Class.new do
|
97
|
-
aspector :class_methods => true do
|
98
|
-
before meth_with_special_char, :do_this
|
99
|
-
end
|
100
|
-
|
101
|
-
class << self
|
102
|
-
def value
|
103
|
-
@value ||= []
|
104
|
-
end
|
105
|
-
|
106
|
-
def do_this *args
|
107
|
-
value << "do_this"
|
108
|
-
end
|
109
|
-
|
110
|
-
define_method meth_with_special_char do |*args|
|
111
|
-
value << "test"
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
klass.send meth_with_special_char, 1, 2
|
117
|
-
klass.value.should == %w"do_this test"
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
end
|
122
|
-
|