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.
Files changed (102) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/.rubocop.yml +26 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +8 -11
  7. data/Changelog.md +59 -0
  8. data/Gemfile +9 -14
  9. data/Gemfile.lock +84 -50
  10. data/README.md +118 -0
  11. data/Rakefile +6 -22
  12. data/aspector.gemspec +15 -127
  13. data/benchmarks/after_benchmark.rb +28 -0
  14. data/benchmarks/around_advice_benchmark.rb +35 -0
  15. data/benchmarks/around_benchmark.rb +32 -0
  16. data/benchmarks/before_benchmark.rb +28 -0
  17. data/benchmarks/benchmark_helper.rb +17 -0
  18. data/benchmarks/combined_benchmark.rb +36 -0
  19. data/benchmarks/method_invocation_benchmark.rb +30 -0
  20. data/benchmarks/raw_benchmark.rb +39 -0
  21. data/examples/activerecord_hooks.rb +10 -15
  22. data/examples/around_example.rb +20 -31
  23. data/examples/aspector_apply_example.rb +10 -17
  24. data/examples/aspector_example.rb +7 -16
  25. data/examples/cache_aspect.rb +20 -30
  26. data/examples/design_by_contract.rb +20 -44
  27. data/examples/exception_handler.rb +12 -20
  28. data/examples/exception_handler2.rb +16 -24
  29. data/examples/implicit_method_option_test.rb +8 -16
  30. data/examples/interception_options_example.rb +71 -0
  31. data/examples/logging_aspect.rb +16 -24
  32. data/examples/process_aspector.rb +13 -0
  33. data/examples/retry_aspect.rb +20 -20
  34. data/lib/aspector.rb +17 -15
  35. data/lib/aspector/advice.rb +44 -57
  36. data/lib/aspector/advice_metadata.rb +10 -11
  37. data/lib/aspector/aspect_instances.rb +2 -3
  38. data/lib/aspector/base.rb +6 -368
  39. data/lib/aspector/base_class_methods.rb +24 -55
  40. data/lib/aspector/deferred_logic.rb +3 -4
  41. data/lib/aspector/deferred_option.rb +5 -10
  42. data/lib/aspector/interception.rb +356 -0
  43. data/lib/aspector/logger.rb +18 -45
  44. data/lib/aspector/logging.rb +10 -29
  45. data/lib/aspector/method_matcher.rb +5 -6
  46. data/lib/aspector/object_extension.rb +4 -12
  47. data/lib/aspector/version.rb +3 -0
  48. data/spec/examples_spec.rb +59 -0
  49. data/spec/functionals/aspect_for_multiple_targets_spec.rb +54 -0
  50. data/spec/functionals/aspect_interception_options_accessing_spec.rb +112 -0
  51. data/spec/functionals/aspect_on_a_class_spec.rb +159 -0
  52. data/spec/functionals/aspect_on_an_instance_spec.rb +66 -0
  53. data/spec/functionals/aspector_spec.rb +138 -0
  54. data/spec/functionals/aspects_combined_spec.rb +37 -0
  55. data/spec/functionals/aspects_execution_order_spec.rb +61 -0
  56. data/spec/functionals/aspects_on_private_methods_spec.rb +82 -0
  57. data/spec/spec_helper.rb +20 -21
  58. data/spec/support/class_builder.rb +44 -0
  59. data/spec/units/advice_spec.rb +49 -0
  60. data/spec/units/advices/after_spec.rb +328 -0
  61. data/spec/units/advices/around_spec.rb +336 -0
  62. data/spec/units/advices/before_filter_spec.rb +287 -0
  63. data/spec/units/advices/before_spec.rb +237 -0
  64. data/spec/units/advices/raw_spec.rb +67 -0
  65. data/spec/units/base_class_methods_spec.rb +262 -0
  66. data/spec/units/base_spec.rb +133 -0
  67. data/spec/units/deferred_logic_spec.rb +35 -0
  68. data/spec/units/logger_spec.rb +20 -0
  69. data/spec/units/logging_spec.rb +85 -0
  70. data/spec/units/method_matcher_spec.rb +95 -0
  71. data/spec/units/object_extension_spec.rb +11 -0
  72. data/spec/units/special_chars_spec.rb +128 -0
  73. metadata +98 -246
  74. data/.document +0 -5
  75. data/.rvmrc +0 -8
  76. data/README.rdoc +0 -80
  77. data/VERSION +0 -1
  78. data/performance-tests/after_test.rb +0 -25
  79. data/performance-tests/around_advice_benchmark.rb +0 -66
  80. data/performance-tests/around_test.rb +0 -27
  81. data/performance-tests/before_test.rb +0 -25
  82. data/performance-tests/combined_test.rb +0 -33
  83. data/performance-tests/method_invocation_test.rb +0 -25
  84. data/performance-tests/raw_test.rb +0 -37
  85. data/performance-tests/test_helper.rb +0 -9
  86. data/run_all_examples.sh +0 -12
  87. data/spec/functional/advices_on_private_methods_spec.rb +0 -21
  88. data/spec/functional/aspect_on_eigen_class_spec.rb +0 -72
  89. data/spec/functional/aspect_on_object_spec.rb +0 -20
  90. data/spec/functional/aspector_spec.rb +0 -140
  91. data/spec/functional/aspects_combined_spec.rb +0 -48
  92. data/spec/functional/execution_order_spec.rb +0 -42
  93. data/spec/unit/advice_spec.rb +0 -4
  94. data/spec/unit/after_spec.rb +0 -88
  95. data/spec/unit/around_spec.rb +0 -76
  96. data/spec/unit/base_class_methods_spec.rb +0 -28
  97. data/spec/unit/base_spec.rb +0 -112
  98. data/spec/unit/before_spec.rb +0 -125
  99. data/spec/unit/deferred_logic_spec.rb +0 -23
  100. data/spec/unit/method_matcher_spec.rb +0 -43
  101. data/spec/unit/raw_spec.rb +0 -53
  102. data/spec/unit/special_chars_spec.rb +0 -122
@@ -1,20 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- describe "Aspector for object" do
4
- it "should work" do
5
- klass = create_test_class
6
-
7
- obj = klass.new
8
-
9
- aspector(obj) do
10
- before :test do value << "do_before" end
11
- end
12
-
13
- obj.test
14
- obj.value.should == %w"do_before test"
15
-
16
- obj2 = klass.new
17
- obj2.test
18
- obj2.value.should == %w"test"
19
- end
20
- end
@@ -1,140 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- describe "Aspector" do
4
- it "should work" do
5
- klass = create_test_class
6
-
7
- aspector(klass) do
8
- before :test do value << "do_before" end
9
-
10
- after :test do |result|
11
- value << "do_after"
12
- result
13
- end
14
-
15
- around :test do |proxy, &block|
16
- value << "do_around_before"
17
- result = proxy.call &block
18
- value << "do_around_after"
19
- result
20
- end
21
- end
22
-
23
- obj = klass.new
24
- obj.test
25
- obj.value.should == %w"do_before do_around_before test do_around_after do_after"
26
- end
27
-
28
- it "multiple aspects should work together" do
29
- klass = create_test_class
30
- aspector(klass) do
31
- before(:test) { value << 'first_aspect' }
32
- end
33
-
34
- aspector(klass) do
35
- before(:test) { value << 'second_aspect' }
36
- end
37
-
38
- obj = klass.new
39
- obj.test
40
- obj.value.should == %w"second_aspect first_aspect test"
41
- end
42
-
43
- it "treating Aspect as regular class should work" do
44
- class TestAspect < Aspector::Base
45
- before(:test) { value << 'before_test' }
46
- end
47
-
48
- klass = create_test_class
49
- TestAspect.apply(klass)
50
-
51
- obj = klass.new
52
- obj.test
53
- obj.value.should == %w"before_test test"
54
- end
55
-
56
- it "can be applied multiple times" do
57
- aspect = Aspector do
58
- before(:test) { value << 'before_test' }
59
- end
60
-
61
- klass = create_test_class
62
- aspect.apply(klass)
63
- aspect.apply(klass)
64
-
65
- obj = klass.new
66
- obj.test
67
- obj.value.should == %w"before_test before_test test"
68
- end
69
-
70
- it "if new_methods_only is true, do not apply to existing methods" do
71
- aspect = Aspector do
72
- before(:test) { value << 'before_test' }
73
- end
74
-
75
- klass = create_test_class
76
- aspect.apply(klass, :new_methods_only => true)
77
-
78
- obj = klass.new
79
- obj.test
80
- obj.value.should == %w"test"
81
- end
82
-
83
- it "if new_methods_only is true, do apply to new methods" do
84
- aspect = Aspector do
85
- before(:test) { value << 'before_test' }
86
- end
87
-
88
- klass = Class.new do
89
- def value
90
- @value ||= []
91
- end
92
- end
93
-
94
- aspect.apply(klass, :new_methods_only => true)
95
-
96
- klass.send :define_method, :test do
97
- value << "test"
98
- end
99
-
100
- obj = klass.new
101
- obj.test
102
- obj.value.should == %w"before_test test"
103
- end
104
-
105
- it "if old_methods_only is true, do apply to methods already defined" do
106
- aspect = Aspector do
107
- before(:test) { value << 'before_test' }
108
- end
109
-
110
- klass = create_test_class
111
- aspect.apply(klass, :old_methods_only => true)
112
-
113
- obj = klass.new
114
- obj.test
115
- obj.value.should == %w"before_test test"
116
- end
117
-
118
- it "if old_methods_only is true, do not apply to new methods" do
119
- aspect = Aspector do
120
- before(:test) { value << 'before_test' }
121
- end
122
-
123
- klass = Class.new do
124
- def value
125
- @value ||= []
126
- end
127
- end
128
-
129
- aspect.apply(klass, :old_methods_only => true)
130
-
131
- klass.send :define_method, :test do
132
- value << "test"
133
- end
134
-
135
- obj = klass.new
136
- obj.test
137
- obj.value.should == %w"test"
138
- end
139
-
140
- end
@@ -1,48 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- describe "Aspects combined" do
4
- it "should work" do
5
- klass = create_test_class
6
-
7
- aspector(klass) do
8
- before :test do value << "do_before" end
9
-
10
- after :test do |result|
11
- value << "do_after"
12
- result
13
- end
14
-
15
- around :test do |proxy, &block|
16
- value << "do_around_before"
17
- result = proxy.call &block
18
- value << "do_around_after"
19
- result
20
- end
21
- end
22
-
23
- aspector(klass) do
24
- before :test do value << "do_before2" end
25
-
26
- after :test do |result|
27
- value << "do_after2"
28
- result
29
- end
30
-
31
- around :test do |proxy, &block|
32
- value << "do_around_before2"
33
- result = proxy.call &block
34
- value << "do_around_after2"
35
- result
36
- end
37
- end
38
-
39
- obj = klass.new
40
- obj.test
41
- obj.value.should == %w"
42
- do_before2 do_around_before2
43
- do_before do_around_before
44
- test
45
- do_around_after do_after
46
- do_around_after2 do_after2"
47
- end
48
- end
@@ -1,42 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- describe "Aspect execution order" do
4
- it "should work" do
5
- klass = create_test_class
6
-
7
- aspector(klass) do
8
- before :test do value << "do_before" end
9
-
10
- after :test do |result|
11
- value << "do_after"
12
- result
13
- end
14
-
15
- around :test do |proxy, &block|
16
- value << "do_around_before"
17
- result = proxy.call &block
18
- value << "do_around_after"
19
- result
20
- end
21
-
22
- before :test do value << "do_before2" end
23
-
24
- after :test do |result|
25
- value << "do_after2"
26
- result
27
- end
28
-
29
- around :test do |proxy, &block|
30
- value << "do_around_before2"
31
- result = proxy.call &block
32
- value << "do_around_after2"
33
- result
34
- end
35
- end
36
-
37
- obj = klass.new
38
- obj.test
39
- obj.value.should == %w"do_before do_before2 do_around_before do_around_before2 test
40
- do_around_after2 do_around_after do_after do_after2"
41
- end
42
- end
@@ -1,4 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- describe "Aspector::Advice" do
4
- end
@@ -1,88 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- describe "After advices" do
4
- it "should work" do
5
- klass = create_test_class do
6
- def do_this result
7
- value << "do_this"
8
- result
9
- end
10
- end
11
-
12
- aspector(klass) do
13
- after :test, :do_this
14
- end
15
-
16
- obj = klass.new
17
- obj.test
18
- obj.value.should == %w"test do_this"
19
- end
20
-
21
- it "method_arg" do
22
- klass = create_test_class do
23
- def do_this method, result
24
- value << "do_this(#{method})"
25
- result
26
- end
27
- end
28
-
29
- aspector(klass) do
30
- after :test, :do_this, :method_arg => true
31
- end
32
-
33
- obj = klass.new
34
- obj.test
35
- obj.value.should == %w"test do_this(test)"
36
- end
37
-
38
- it "new result will be returned by default" do
39
- klass = create_test_class
40
-
41
- aspector(klass) do
42
- after :test do |result|
43
- value << "do_after"
44
- 'do_after'
45
- end
46
- end
47
-
48
- obj = klass.new
49
- obj.test.should == 'do_after'
50
- obj.value.should == %w"test do_after"
51
- end
52
-
53
- it "result_arg set to false" do
54
- klass = create_test_class do
55
- def test
56
- value << "test"
57
- 'test'
58
- end
59
- end
60
-
61
- aspector(klass) do
62
- after :test, :result_arg => false do
63
- value << "do_after"
64
- 'do_after'
65
- end
66
- end
67
-
68
- obj = klass.new
69
- obj.test.should == 'test'
70
- obj.value.should == %w"test do_after"
71
- end
72
-
73
- it "logic in block" do
74
- klass = create_test_class
75
-
76
- aspector(klass) do
77
- after(:test) do |result|
78
- value << 'do_block'
79
- result
80
- end
81
- end
82
-
83
- obj = klass.new
84
- obj.test
85
- obj.value.should == %w"test do_block"
86
- end
87
- end
88
-
@@ -1,76 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- describe "Around advices" do
4
- it "should work" do
5
- klass = create_test_class do
6
- def do_this proxy, &block
7
- value << "before"
8
- result = proxy.call &block
9
- value << "after"
10
- result
11
- end
12
- end
13
-
14
- aspector(klass) do
15
- around :test, :do_this
16
- end
17
-
18
- obj = klass.new
19
- obj.test
20
- obj.value.should == %w"before test after"
21
- end
22
-
23
- it "logic in String" do
24
- klass = create_test_class
25
-
26
- aspector(klass) do
27
- around :test, <<-CODE
28
- value << "before"
29
- result = INVOKE_PROXY
30
- value << "after"
31
- result
32
- CODE
33
- end
34
-
35
- obj = klass.new
36
- obj.test
37
- obj.value.should == %w"before test after"
38
- end
39
-
40
- it "logic in block" do
41
- klass = create_test_class
42
-
43
- aspector(klass) do
44
- around :test do |proxy, &block|
45
- value << "before"
46
- result = proxy.call &block
47
- value << "after"
48
- result
49
- end
50
- end
51
-
52
- obj = klass.new
53
- obj.test
54
- obj.value.should == %w"before test after"
55
- end
56
-
57
- it "method_arg" do
58
- klass = create_test_class do
59
- def do_this method, proxy, &block
60
- value << "before(#{method})"
61
- result = proxy.call &block
62
- value << "after(#{method})"
63
- result
64
- end
65
- end
66
-
67
- aspector(klass) do
68
- around :test, :do_this, :method_arg => true
69
- end
70
-
71
- obj = klass.new
72
- obj.test
73
- obj.value.should == %w"before(test) test after(test)"
74
- end
75
- end
76
-
@@ -1,28 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- describe "Aspector::Base class methods" do
4
- it "before" do
5
- klass = Class.new(Aspector::Base) do
6
- before :test, :do_before
7
- end
8
-
9
- klass.send(:advices).size.should == 1
10
- advice = klass.send(:advices).first
11
- advice.before?.should be_true
12
- advice.options[:skip_if_false].should_not be_true
13
- advice.with_method.should == :do_before
14
- end
15
-
16
- it "before_filter" do
17
- klass = Class.new(Aspector::Base) do
18
- before_filter :test, :do_before
19
- end
20
-
21
- klass.send(:advices).size.should == 1
22
- advice = klass.send(:advices).first
23
- advice.before?.should be_true
24
- advice.options[:skip_if_false].should be_true
25
- advice.with_method.should == :do_before
26
- end
27
-
28
- end