aspector 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.irbrc ADDED
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
2
+
3
+ require 'aspector'
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1,3 @@
1
+ rvm_gemset_create_on_use_flag=1
2
+
3
+ rvm use 1.8.7@aspector
data/Gemfile ADDED
@@ -0,0 +1,20 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "rspec", "~> 2.3.0"
5
+ gem "mocha", "~> 0.10.0"
6
+ gem "bundler", "~> 1.0.0"
7
+ gem "jeweler", "~> 1.6.4"
8
+ gem "rcov", ">= 0"
9
+ gem "reek", "~> 1.2.8"
10
+ gem "roodi", "~> 2.1.0"
11
+
12
+ gem "guard", "~> 0.8.4"
13
+ gem "guard-bundler", "~> 0.1.3"
14
+ gem "guard-rspec", "~> 0.5.2"
15
+ gem "guard-shell", "~> 0.1.1"
16
+ gem 'rb-fsevent', "~> 0.4.3.1"
17
+ gem 'growl', "~> 1.0.3"
18
+
19
+ gem 'awesome_print'
20
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,66 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ awesome_print (0.4.0)
5
+ diff-lcs (1.1.3)
6
+ git (1.2.5)
7
+ growl (1.0.3)
8
+ guard (0.8.8)
9
+ thor (~> 0.14.6)
10
+ guard-bundler (0.1.3)
11
+ bundler (>= 1.0.0)
12
+ guard (>= 0.2.2)
13
+ guard-rspec (0.5.2)
14
+ guard (>= 0.8.4)
15
+ guard-shell (0.1.1)
16
+ guard (>= 0.2.0)
17
+ jeweler (1.6.4)
18
+ bundler (~> 1.0)
19
+ git (>= 1.2.5)
20
+ rake
21
+ metaclass (0.0.1)
22
+ mocha (0.10.0)
23
+ metaclass (~> 0.0.1)
24
+ rake (0.9.2.2)
25
+ rb-fsevent (0.4.3.1)
26
+ rcov (0.9.11)
27
+ reek (1.2.8)
28
+ ruby2ruby (~> 1.2)
29
+ ruby_parser (~> 2.0)
30
+ sexp_processor (~> 3.0)
31
+ roodi (2.1.0)
32
+ ruby_parser
33
+ rspec (2.3.0)
34
+ rspec-core (~> 2.3.0)
35
+ rspec-expectations (~> 2.3.0)
36
+ rspec-mocks (~> 2.3.0)
37
+ rspec-core (2.3.1)
38
+ rspec-expectations (2.3.0)
39
+ diff-lcs (~> 1.1.2)
40
+ rspec-mocks (2.3.0)
41
+ ruby2ruby (1.3.1)
42
+ ruby_parser (~> 2.0)
43
+ sexp_processor (~> 3.0)
44
+ ruby_parser (2.3.1)
45
+ sexp_processor (~> 3.0)
46
+ sexp_processor (3.0.7)
47
+ thor (0.14.6)
48
+
49
+ PLATFORMS
50
+ ruby
51
+
52
+ DEPENDENCIES
53
+ awesome_print
54
+ bundler (~> 1.0.0)
55
+ growl (~> 1.0.3)
56
+ guard (~> 0.8.4)
57
+ guard-bundler (~> 0.1.3)
58
+ guard-rspec (~> 0.5.2)
59
+ guard-shell (~> 0.1.1)
60
+ jeweler (~> 1.6.4)
61
+ mocha (~> 0.10.0)
62
+ rb-fsevent (~> 0.4.3.1)
63
+ rcov
64
+ reek (~> 1.2.8)
65
+ roodi (~> 2.1.0)
66
+ rspec (~> 2.3.0)
data/Guardfile ADDED
@@ -0,0 +1,13 @@
1
+ # More info at https://github.com/guard/guard#readme
2
+
3
+ guard 'bundler' do
4
+ watch('Gemfile')
5
+ end
6
+
7
+ #guard 'shell' do
8
+ # watch(%r{^(lib|spec)/.+\.rb$}) { `rspec spec` }
9
+ #end
10
+
11
+ guard 'rspec', :notification => false do
12
+ watch(%r{^(lib|spec)/.+\.rb$}) { "spec" }
13
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Guoliang Cao
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,66 @@
1
+ = aspector
2
+
3
+ aspector = ASPECT Oriented Ruby programming
4
+
5
+ == Highlights
6
+
7
+ * Encapsulate logic as aspects and apply to multiple targets easily
8
+ * Support before/before_filter/after/around advices
9
+ * Work anywhere - inside/outside the target class, before/after methods are created
10
+ * Small codebase, intuitive API
11
+
12
+ == Installation
13
+
14
+ gem install aspector
15
+
16
+ == Examples
17
+
18
+ require 'aspector'
19
+
20
+ class A
21
+ def test
22
+ puts 'test'
23
+ end
24
+ end
25
+
26
+ ##############################
27
+
28
+ TestAspect = Aspector do
29
+ target "
30
+ def do_this
31
+ puts 'do_this'
32
+ end
33
+ "
34
+
35
+ before :test, :do_this
36
+ before :test do
37
+ puts 'do_that'
38
+ end
39
+ end
40
+
41
+ TestAspect.apply(A)
42
+
43
+ ##############################
44
+
45
+ A.new.test
46
+
47
+ # Expected output:
48
+ # do_this
49
+ # do_that
50
+ # test
51
+
52
+ == Contributing to aspector
53
+
54
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
55
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
56
+ * Fork the project
57
+ * Start a feature/bugfix branch
58
+ * Commit and push until you are happy with your contribution
59
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
60
+ * 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.
61
+
62
+ == Copyright
63
+
64
+ Copyright (c) 2011 Guoliang Cao. See LICENSE.txt for
65
+ further details.
66
+
data/Rakefile ADDED
@@ -0,0 +1,62 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "aspector"
18
+ gem.homepage = "http://github.com/gcao/aspector"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Aspect Oriented Ruby Programming}
21
+ gem.description = %Q{}
22
+ gem.email = "gcao99@gmail.com"
23
+ gem.authors = ["Guoliang Cao"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ require 'reek/rake/task'
40
+ Reek::Rake::Task.new do |t|
41
+ t.fail_on_error = true
42
+ t.verbose = false
43
+ t.source_files = 'lib/**/*.rb'
44
+ end
45
+
46
+ require 'roodi'
47
+ require 'roodi_task'
48
+ RoodiTask.new do |t|
49
+ t.verbose = false
50
+ end
51
+
52
+ task :default => :spec
53
+
54
+ require 'rake/rdoctask'
55
+ Rake::RDocTask.new do |rdoc|
56
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
57
+
58
+ rdoc.rdoc_dir = 'rdoc'
59
+ rdoc.title = "aspector #{version}"
60
+ rdoc.rdoc_files.include('README*')
61
+ rdoc.rdoc_files.include('lib/**/*.rb')
62
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.5.0
@@ -0,0 +1,39 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+
3
+ require 'aspector'
4
+
5
+ class A
6
+ def test
7
+ puts 'test'
8
+ end
9
+ end
10
+
11
+ ##############################
12
+
13
+ aspector(A) do
14
+ target "
15
+ def do_this &block
16
+ puts 'before'
17
+ block.call
18
+ puts 'after'
19
+ end
20
+ "
21
+
22
+ around :test, :do_this
23
+ around :test do |&block|
24
+ puts 'before(block)'
25
+ block.call
26
+ puts 'after(block)'
27
+ end
28
+ end
29
+
30
+ ##############################
31
+
32
+ A.new.test
33
+
34
+ # Expected output:
35
+ # before(block)
36
+ # before
37
+ # test
38
+ # after
39
+ # after(block)
@@ -0,0 +1,35 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+
3
+ require 'aspector'
4
+
5
+ class A
6
+ def test
7
+ puts 'test'
8
+ end
9
+ end
10
+
11
+ ##############################
12
+
13
+ TestAspect = Aspector do
14
+ target "
15
+ def do_this
16
+ puts 'do_this'
17
+ end
18
+ "
19
+
20
+ before :test, :do_this
21
+ before :test do
22
+ puts 'do_that'
23
+ end
24
+ end
25
+
26
+ TestAspect.apply(A)
27
+
28
+ ##############################
29
+
30
+ A.new.test
31
+
32
+ # Expected output:
33
+ # do_this
34
+ # do_that
35
+ # test
@@ -0,0 +1,33 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+
3
+ require 'aspector'
4
+
5
+ class A
6
+ def test
7
+ puts 'test'
8
+ end
9
+ end
10
+
11
+ ##############################
12
+
13
+ aspector(A) do
14
+ target "
15
+ def do_this
16
+ puts 'do_this'
17
+ end
18
+ "
19
+
20
+ before :test, :do_this
21
+ before :test do
22
+ puts 'do_that'
23
+ end
24
+ end
25
+
26
+ ##############################
27
+
28
+ A.new.test
29
+
30
+ # Expected output:
31
+ # do_this
32
+ # do_that
33
+ # test
@@ -0,0 +1,76 @@
1
+ module Aspector
2
+ class Advice
3
+
4
+ BEFORE = 1
5
+ AFTER = 2
6
+ AROUND = 3
7
+
8
+ attr_reader :type, :method_matcher, :options, :advice_block
9
+
10
+ def initialize parent, type, method_matcher, with_method, options = {}, &block
11
+ @parent = parent
12
+ @type = type
13
+ @method_matcher = method_matcher
14
+ @with_method = with_method
15
+ @options = options
16
+ @advice_block = block.to_proc if block_given?
17
+ end
18
+
19
+ def with_method
20
+ @with_method ||= "aspect_#{@parent.hash}_#{@parent.advices.index(self)}"
21
+ end
22
+
23
+ def match? method, context = nil
24
+ return if method == with_method
25
+ return unless @method_matcher.match?(method, context)
26
+
27
+ return true unless @options[:except]
28
+
29
+ @except ||= MethodMatcher.new([@options[:except]].flatten)
30
+
31
+ not @except.match?(method)
32
+ end
33
+
34
+ def before?
35
+ type == BEFORE and not options[:skip_if_false]
36
+ end
37
+
38
+ def before_filter?
39
+ type == BEFORE and options[:skip_if_false]
40
+ end
41
+
42
+ def after?
43
+ type == AFTER
44
+ end
45
+
46
+ def around?
47
+ type == AROUND
48
+ end
49
+
50
+ def invoke obj, *args, &block
51
+ obj.send with_method, *args, &block
52
+ end
53
+
54
+ def type_name
55
+ case @type
56
+ when BEFORE then @options[:skip_if_false] ? "BEFORE_FILTER" : "BEFORE"
57
+ when AFTER then "AFTER"
58
+ when AROUND then "AROUND"
59
+ else "UNKNOWN?!"
60
+ end
61
+ end
62
+
63
+ def to_s
64
+ s = type_name
65
+ s << " [" << @method_matcher.to_s << "] DO "
66
+ s << @with_method.to_s
67
+ s << " WITH OPTIONS "
68
+ @options.each do |key, value|
69
+ next if key == :skip_if_false
70
+ s << key.to_s << ":" << value.to_s << " "
71
+ end
72
+ s
73
+ end
74
+
75
+ end
76
+ end
@@ -0,0 +1,17 @@
1
+ module Aspector
2
+ class AdviceMetadata
3
+ attr_reader :advice_type, :default_options, :mandatory_options
4
+
5
+ def initialize advice_type, default_options, mandatory_options
6
+ @advice_type = advice_type
7
+ @default_options = default_options || {}
8
+ @mandatory_options = mandatory_options || {}
9
+ end
10
+
11
+ BEFORE = new Aspector::Advice::BEFORE, nil, :skip_if_false => false
12
+ BEFORE_FILTER = new Aspector::Advice::BEFORE, nil, :skip_if_false => true
13
+ AFTER = new Aspector::Advice::AFTER, { :result_arg => true }, nil
14
+ AROUND = new Aspector::Advice::AROUND, nil, nil
15
+ end
16
+ end
17
+
@@ -0,0 +1,75 @@
1
+ require 'erb'
2
+
3
+ module Aspector
4
+ class Aspect
5
+
6
+ attr_reader :advices, :options, :deferred_logics
7
+
8
+ def initialize options = {}, &block
9
+ @options = options
10
+ @advices = []
11
+ instance_eval &block
12
+ end
13
+
14
+ def apply target, options = {}
15
+ aspect_instance = AspectInstance.new(target, self, options)
16
+ aspect_instance.apply
17
+ end
18
+
19
+ def before *methods, &block
20
+ @advices << create_advice(Aspector::AdviceMetadata::BEFORE, self, methods, &block)
21
+ end
22
+
23
+ def before_filter *methods, &block
24
+ @advices << create_advice(Aspector::AdviceMetadata::BEFORE_FILTER, self, methods, &block)
25
+ end
26
+
27
+ def after *methods, &block
28
+ @advices << create_advice(Aspector::AdviceMetadata::AFTER, self, methods, &block)
29
+ end
30
+
31
+ def around *methods, &block
32
+ @advices << create_advice(Aspector::AdviceMetadata::AROUND, self, methods, &block)
33
+ end
34
+
35
+ def target code
36
+ logic = DeferredLogic.new(code)
37
+ @deferred_logics ||= []
38
+ @deferred_logics << logic
39
+ logic
40
+ end
41
+
42
+ def to_hash
43
+ {
44
+ "type" => self.class.name,
45
+ "options" => @options,
46
+ "advices" => @advices.map {|advice| advice.to_s }
47
+ }
48
+ end
49
+
50
+ private
51
+
52
+ def create_advice meta_data, klass_or_module, *methods, &block
53
+ methods.flatten!
54
+
55
+ options = meta_data.default_options.clone
56
+ options.merge!(methods.pop) if methods.last.is_a? Hash
57
+ options.merge!(meta_data.mandatory_options)
58
+
59
+ # Convert symbols to strings to avoid inconsistencies
60
+ methods.size.times do |i|
61
+ methods[i] = methods[i].to_s if methods[i].is_a? Symbol
62
+ end
63
+
64
+ with_method = methods.pop unless block_given?
65
+
66
+ Aspector::Advice.new(self,
67
+ meta_data.advice_type,
68
+ Aspector::MethodMatcher.new(*methods),
69
+ with_method,
70
+ options,
71
+ &block)
72
+ end
73
+ end
74
+ end
75
+