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,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
-
@@ -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
-
@@ -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
-