aspector 0.13.1 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
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
data/.document DELETED
@@ -1,5 +0,0 @@
1
- lib/**/*.rb
2
- bin/*
3
- -
4
- features/**/*.feature
5
- LICENSE.txt
data/.rvmrc DELETED
@@ -1,8 +0,0 @@
1
- rvm_gemset_create_on_use_flag=1
2
-
3
- if [ "$RUBY_VERSION" '==' "1.8" ]; then
4
- rvm use 1.8.7@aspector
5
- else
6
- rvm use 1.9.3@aspector
7
- fi
8
-
@@ -1,80 +0,0 @@
1
- = aspector
2
- {<img src="https://secure.travis-ci.org/gcao/aspector.png" />}[http://travis-ci.org/gcao/aspector]
3
-
4
- aspector = ASPECT Oriented Ruby programming
5
-
6
- == Highlights
7
-
8
- * Encapsulate logic as aspects and apply to multiple targets easily
9
- * Support before/before_filter/after/around advices
10
- * Work anywhere - inside/outside the target class, before/after methods are created
11
- * Small codebase, intuitive API
12
-
13
- == Installation
14
-
15
- gem install aspector
16
-
17
- == Examples
18
-
19
- class A
20
- def test
21
- puts 'test'
22
- end
23
- end
24
-
25
- ##############################
26
-
27
- require 'aspector'
28
-
29
- class TestAspect < Aspector::Base
30
- target do
31
- def do_this
32
- puts 'do_this'
33
- end
34
- end
35
-
36
- before options[:method], :do_this
37
-
38
- before options[:method] do
39
- puts 'do_that'
40
- end
41
- end
42
-
43
- ##############################
44
-
45
- TestAspect.apply A, :method => :test
46
-
47
- A.new.test
48
-
49
- # Expected output:
50
- # do_this
51
- # do_that
52
- # test
53
-
54
- == Notes
55
- To use a different logger, e.g. Logem::Logger, run below command
56
-
57
- ASPECTOR_LOGGER=Logem::Logger LOGEM_LOG_LEVEL=trace ruby -rlogem examples/cache_aspect.rb
58
-
59
- Explanation:
60
- ASPECTOR_LOGGER=Logem::Logger => set logger to Logem::Logger class
61
- LOGEM_LOG_LEVEL=trace => set logem log level to trace
62
- -rlogem => require logem otherwise it might not be available
63
- when the logger is initialized. This could be handled by
64
- bundler etc though.
65
-
66
- == Contributing to aspector
67
-
68
- * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
69
- * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
70
- * Fork the project
71
- * Start a feature/bugfix branch
72
- * Commit and push until you are happy with your contribution
73
- * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
74
- * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
75
-
76
- == Copyright
77
-
78
- Copyright (c) 2011 Guoliang Cao. See LICENSE.txt for
79
- further details.
80
-
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.13.1
@@ -1,25 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
-
3
- class AfterTest < Test::Unit::TestCase
4
- include RubyProf::Test
5
-
6
- class Klass
7
-
8
- aspector do
9
- after :test, :after_test
10
- end
11
-
12
- def test_no_aspect; end
13
-
14
- def test; end
15
-
16
- def after_test result; end
17
- end
18
-
19
- def test_after
20
- o = Klass.new
21
- o.test_no_aspect
22
- o.test
23
- end
24
- end
25
-
@@ -1,66 +0,0 @@
1
- class A
2
- def test input
3
- input.upcase!
4
- end
5
- end
6
-
7
- class B
8
- def test input
9
- input.upcase!
10
- end
11
- end
12
-
13
- ##############################
14
-
15
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
16
-
17
- require 'aspector'
18
-
19
- class AroundAspect < Aspector::Base
20
- around :test do |proxy, *args, &block|
21
- begin
22
- proxy.call *args, &block
23
- rescue => e
24
- end
25
- end
26
- end
27
-
28
- class RawAspect < Aspector::Base
29
- raw :test do |method,|
30
- eval <<-CODE
31
- alias #{method}_without_aspect #{method}
32
-
33
- def #{method} *args, &block
34
- #{method}_without_aspect *args, &block
35
- rescue => e
36
- end
37
- CODE
38
- end
39
- end
40
-
41
- ##############################
42
-
43
- AroundAspect.apply(A)
44
- RawAspect.apply(B)
45
-
46
- a = A.new
47
- b = B.new
48
-
49
- require 'benchmark'
50
-
51
- TIMES = 100000
52
- Benchmark.bmbm do |x|
53
- x.report "Around advice - good" do
54
- TIMES.times { a.test 'good' }
55
- end
56
- x.report "Around advice - bad" do
57
- TIMES.times { a.test nil }
58
- end
59
- x.report "Raw - good" do
60
- TIMES.times { b.test 'good' }
61
- end
62
- x.report "Raw - bad" do
63
- TIMES.times { b.test nil }
64
- end
65
- end
66
-
@@ -1,27 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
-
3
- class AroundTest < Test::Unit::TestCase
4
- include RubyProf::Test
5
-
6
- class Klass
7
-
8
- aspector do
9
- around :test, :around_test
10
- end
11
-
12
- def test_no_aspect; end
13
-
14
- def test; end
15
-
16
- def around_test proxy, &block
17
- proxy.call &block
18
- end
19
- end
20
-
21
- def test_around
22
- o = Klass.new
23
- o.test_no_aspect
24
- o.test
25
- end
26
- end
27
-
@@ -1,25 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
-
3
- class BeforeTest < Test::Unit::TestCase
4
- include RubyProf::Test
5
-
6
- class Klass
7
-
8
- aspector do
9
- before :test, :before_test
10
- end
11
-
12
- def test_no_aspect; end
13
-
14
- def test; end
15
-
16
- def before_test; end
17
- end
18
-
19
- def test_before
20
- o = Klass.new
21
- o.test_no_aspect
22
- o.test
23
- end
24
- end
25
-
@@ -1,33 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
-
3
- class CombinedTest < Test::Unit::TestCase
4
- include RubyProf::Test
5
-
6
- class Test
7
-
8
- aspector do
9
- before :test, :before_test
10
- after :test, :after_test
11
- around :test, :around_test
12
- end
13
-
14
- def test_no_aspect; end
15
-
16
- def test; end
17
-
18
- def before_test; end
19
-
20
- def after_test result; end
21
-
22
- def around_test &block
23
- block.call
24
- end
25
- end
26
-
27
- def test_combined
28
- o = Test.new
29
- o.test_no_aspect
30
- o.test
31
- end
32
- end
33
-
@@ -1,25 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
-
3
- class MethodInvokationTest < Test::Unit::TestCase
4
- include RubyProf::Test
5
-
6
- class Klass
7
- def do_something; end
8
-
9
- def test
10
- do_something
11
- end
12
-
13
- do_something_method = instance_method(:do_something)
14
-
15
- define_method :test_with_method_object do
16
- do_something_method.bind(self).call
17
- end
18
- end
19
-
20
- def test_method_invocation
21
- o = Klass.new
22
- o.test
23
- o.test_with_method_object
24
- end
25
- end
@@ -1,37 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
-
3
- class RawTest < Test::Unit::TestCase
4
- include RubyProf::Test
5
-
6
- class Klass
7
-
8
- aspector do
9
-
10
- raw :test do |method, aspect|
11
- eval <<-CODE
12
- alias #{method}_without_aspect #{method}
13
-
14
- define_method :#{method} do
15
- return #{method}_without_aspect if aspect.disabled?
16
- before_#{method}
17
- #{method}_without_aspect
18
- end
19
- CODE
20
- end
21
-
22
- end
23
-
24
- def test_no_aspect; end
25
-
26
- def test; end
27
-
28
- def before_test; end
29
- end
30
-
31
- def test_raw
32
- o = Klass.new
33
- o.test_no_aspect
34
- o.test
35
- end
36
- end
37
-
@@ -1,9 +0,0 @@
1
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
-
3
- require 'rubygems'
4
- require 'test/unit'
5
- require 'ruby-prof/test'
6
- require 'aspector'
7
-
8
- RubyProf::Test::PROFILE_OPTIONS[:count ] = 200000
9
- RubyProf::Test::PROFILE_OPTIONS[:output_dir] = File.dirname(__FILE__) + "/output"
@@ -1,12 +0,0 @@
1
- #!/bin/sh
2
-
3
- for f in examples/*
4
- do
5
- echo
6
- echo === Running $f ===
7
- echo
8
- ruby $f
9
- echo
10
- echo
11
- done
12
-
@@ -1,21 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- describe "Advices on private methods" do
4
- it "should work" do
5
- klass = create_test_class do
6
- private :test
7
- end
8
-
9
- aspector(klass) do
10
- before :test do value << "do_before(public_methods_only)" end
11
- end
12
-
13
- aspector(klass, :private_methods => true) do
14
- before :test do value << "do_before" end
15
- end
16
-
17
- obj = klass.new
18
- obj.send :test
19
- obj.value.should == %w"do_before test"
20
- end
21
- end
@@ -1,72 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- describe "Aspector for eigen class" do
4
- it "should work" do
5
- klass = Class.new do
6
- class << self
7
- def value
8
- @value ||= []
9
- end
10
-
11
- def test
12
- value << "test"
13
- end
14
- end
15
- end
16
-
17
- aspector(klass, :class_methods => true) do
18
- before :test do value << "do_before" end
19
-
20
- after :test do |result|
21
- value << "do_after"
22
- result
23
- end
24
-
25
- around :test do |proxy, &block|
26
- value << "do_around_before"
27
- result = proxy.call &block
28
- value << "do_around_after"
29
- result
30
- end
31
- end
32
-
33
- klass.test
34
- klass.value.should == %w"do_before do_around_before test do_around_after do_after"
35
- end
36
-
37
- it "new methods" do
38
- klass = Class.new do
39
- class << self
40
- def value
41
- @value ||= []
42
- end
43
- end
44
- end
45
-
46
- aspector(klass, :class_methods => true) do
47
- before :test do value << "do_before" end
48
-
49
- after :test do |result|
50
- value << "do_after"
51
- result
52
- end
53
-
54
- around :test do |proxy, &block|
55
- value << "do_around_before"
56
- result = proxy.call &block
57
- value << "do_around_after"
58
- result
59
- end
60
- end
61
-
62
- klass.instance_eval do
63
- def test
64
- value << "test"
65
- end
66
- end
67
-
68
- klass.test
69
- klass.value.should == %w"do_before do_around_before test do_around_after do_after"
70
- end
71
-
72
- end