tivoli 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm --create 1.9.2@tivoli
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source :rubygems
2
+
3
+ gem 'rspec'
4
+ gem "jeweler", "~> 1.6.4"
5
+ gem 'rake','0.8.7'
6
+
@@ -0,0 +1,26 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.6.4)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.8.7)
11
+ rspec (2.6.0)
12
+ rspec-core (~> 2.6.0)
13
+ rspec-expectations (~> 2.6.0)
14
+ rspec-mocks (~> 2.6.0)
15
+ rspec-core (2.6.4)
16
+ rspec-expectations (2.6.0)
17
+ diff-lcs (~> 1.1.2)
18
+ rspec-mocks (2.6.0)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ jeweler (~> 1.6.4)
25
+ rake (= 0.8.7)
26
+ rspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Erik Fonselius
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.
@@ -0,0 +1,51 @@
1
+ = Tivoli
2
+
3
+ http://en.wikipedia.org/wiki/Aspect-oriented_programming
4
+
5
+ == Usage
6
+
7
+ t = Tivoli.new(String.instance_method(:+))
8
+ t.aspect :before { |time, args, &block|
9
+ # log stuff here or
10
+ puts "LOG #{args}"
11
+ }
12
+ "hello" + "other" # => 'helloother'
13
+ LOG ['other']
14
+
15
+ t.aspect :before { |time, args, &block|
16
+ # change arguments
17
+ args[0].reverse!
18
+ }
19
+ 'hello' + 'other' # => 'helloretho'
20
+
21
+ # prev is used for chaining multiple filters, first time prev is nil
22
+ t.filter :before { |prev, time, args, &block|
23
+ # change result
24
+ 'nope'
25
+ }
26
+ 'hello' + 'other' # => 'nope'
27
+
28
+
29
+ t.aspect :before {
30
+ sleep 1
31
+ }
32
+ t.filter :after { |prev, time, args, &block|
33
+ prev + time
34
+ }
35
+ "hello" + "other" # => 'helloother1000'
36
+
37
+ == Contributing to tivoli
38
+
39
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
40
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
41
+ * Fork the project
42
+ * Start a feature/bugfix branch
43
+ * Commit and push until you are happy with your contribution
44
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
45
+ * 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.
46
+
47
+ == Copyright
48
+
49
+ Copyright (c) 2011 Erik Fonselius. See LICENSE.txt for
50
+ further details.
51
+
@@ -0,0 +1,49 @@
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 = "tivoli"
18
+ gem.homepage = "http://github.com/Fonsan/tivoli"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{AOP in 100 lines of ruby}
21
+ gem.description = %Q{AOP in 100 lines of ruby}
22
+ gem.email = "fonsan@gmail.com"
23
+ gem.authors = ["Erik Fonselius"]
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
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "tivoli #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,33 @@
1
+
2
+
3
+ list = ObjectSpace.each_object.select do |o|
4
+ o.class == Class || o.class == Module
5
+ end
6
+
7
+ methods = []
8
+ list.each do |c|
9
+ methods += c.methods.map do |m|
10
+ c.method(m)
11
+ end
12
+ methods += c.instance_methods.map do |m|
13
+ c.instance_method(m)
14
+ end
15
+ end
16
+
17
+ puts methods.count
18
+
19
+ require './lib/tivoli'
20
+
21
+ methods.each do |m|
22
+ begin
23
+ unless m.owner == Method
24
+ t = Tivoli.new(m)
25
+ t.complete { |*args|
26
+ args.unshift m.name
27
+ puts args.inspect
28
+ }
29
+ end
30
+ rescue NoMethodError => e
31
+
32
+ end
33
+ end
@@ -0,0 +1,76 @@
1
+ class Tivoli
2
+ attr_accessor :method, :filters, :aspects
3
+
4
+ def self.bench
5
+ start = Time.now.to_f
6
+ time_passed = proc {
7
+ time_taken = ((Time.now.to_f - start)*1000).to_i
8
+ }
9
+ yield time_passed
10
+ time_passed.call
11
+ end
12
+
13
+ def run(fallback, &block)
14
+ key = :"tivoli_#{object_id}"
15
+ return fallback.call if Thread.current[key]
16
+ Thread.current[key] = true
17
+ result = block.call
18
+ Thread.current[key] = false
19
+ result
20
+ end
21
+
22
+ def initialize(method)
23
+ @method = method
24
+ default_to_array = proc { |h,k| h[k] = [] }
25
+ self.aspects = Hash.new(&default_to_array)
26
+ self.filters = Hash.new(&default_to_array)
27
+
28
+ tivoli = self
29
+
30
+ method.owner.send(:define_method, method.name) do |*args, &blk|
31
+ result = nil
32
+
33
+ call_original_method = proc {
34
+ method.bind(self)[*args, &blk]
35
+ }
36
+
37
+ tivoli.run(call_original_method) do
38
+ time = Tivoli.bench do |time_passed|
39
+ execute = proc { |state, &block|
40
+ tivoli.aspects[state].each do |a|
41
+ a.call(time_passed.call, args, &blk)
42
+ end
43
+ if tivoli.filters[state].empty?
44
+ block.call
45
+ else
46
+ tivoli.filters[state].reduce(result) do |prev, f|
47
+ f.call(prev, time_passed.call, args, &blk)
48
+ end
49
+ end
50
+ }
51
+ result = execute.call :before, &call_original_method
52
+ result = execute.call :after do
53
+ result
54
+ end
55
+ end
56
+ end
57
+ result
58
+ end
59
+ end
60
+
61
+ def aspect(state, &block)
62
+ aspects[state].push(block)
63
+ end
64
+
65
+ def filter(state, &block)
66
+ filters[state].push(block)
67
+ end
68
+
69
+ def stop
70
+ aspects.clear
71
+ filters.clear
72
+ method.owner.send(:define_method, method.name) do |*args, &blk|
73
+ method.bind(self)[*args, &blk]
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'tivoli'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
@@ -0,0 +1,72 @@
1
+ require './lib/tivoli'
2
+ describe Tivoli do
3
+ before :each do
4
+ @t = Tivoli.new String.instance_method(:upcase)
5
+ end
6
+
7
+ it 'should log' do
8
+ i = 0
9
+ task = proc { |*args|
10
+ i += 1
11
+ }
12
+ @t.aspect :before, &task
13
+ @t.aspect :after, &task
14
+ "aBc".upcase
15
+ i.should == 2
16
+ end
17
+
18
+
19
+ describe 'upcase' do
20
+ it 'should be able to proxy upcase' do
21
+ t = Tivoli.new(String.instance_method(:upcase))
22
+ t.filter :after do |prev|
23
+ prev.reverse
24
+ end
25
+ "hello".upcase.should == 'OLLEH'
26
+ end
27
+ end
28
+
29
+ describe 'String#+' do
30
+ before :each do
31
+ @t.stop if @t
32
+ @t = Tivoli.new(String.instance_method(:+))
33
+ end
34
+
35
+ it 'should log' do
36
+ log = nil
37
+ @t.aspect :before do |time, args, &block|
38
+ # log stuff here or
39
+ log = "LOG #{args}"
40
+ end
41
+ ("hello" + "other").should == 'helloother'
42
+ log.should == 'LOG ["other"]'
43
+ end
44
+
45
+ it 'should change arguments' do
46
+ @t.aspect :before do |time, args, &block|
47
+ # change arguments
48
+ args[0].reverse!
49
+ end
50
+ ('hello' + 'other').should == 'hellorehto'
51
+ end
52
+
53
+ it 'should chain' do
54
+
55
+ @t.filter :before do |prev, time, args, &block|
56
+ 'nope'
57
+ end
58
+ @t.filter :before do |prev|
59
+ prev.reverse
60
+ end
61
+ ('hello' + 'other').should == 'epon'
62
+ end
63
+
64
+ it "should filter after" do
65
+ @t.filter :after do |prev, time, args, &block|
66
+ prev + time
67
+ end
68
+ "hello" + "other" # => 'helloother0'
69
+ end
70
+ end
71
+
72
+ end
@@ -0,0 +1,59 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "tivoli"
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Erik Fonselius"]
12
+ s.date = "2012-05-10"
13
+ s.description = "AOP in 100 lines of ruby"
14
+ s.email = "fonsan@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ ".rvmrc",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.rdoc",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "examples/everything.rb",
30
+ "lib/tivoli.rb",
31
+ "spec/spec_helper.rb",
32
+ "spec/tivoli_spec.rb",
33
+ "tivoli.gemspec"
34
+ ]
35
+ s.homepage = "http://github.com/Fonsan/tivoli"
36
+ s.licenses = ["MIT"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = "1.8.21"
39
+ s.summary = "AOP in 100 lines of ruby"
40
+
41
+ if s.respond_to? :specification_version then
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ s.add_runtime_dependency(%q<rspec>, [">= 0"])
46
+ s.add_runtime_dependency(%q<jeweler>, ["~> 1.6.4"])
47
+ s.add_runtime_dependency(%q<rake>, ["= 0.8.7"])
48
+ else
49
+ s.add_dependency(%q<rspec>, [">= 0"])
50
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
51
+ s.add_dependency(%q<rake>, ["= 0.8.7"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<rspec>, [">= 0"])
55
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
56
+ s.add_dependency(%q<rake>, ["= 0.8.7"])
57
+ end
58
+ end
59
+
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tivoli
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Erik Fonselius
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: jeweler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.6.4
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.6.4
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - '='
52
+ - !ruby/object:Gem::Version
53
+ version: 0.8.7
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.8.7
62
+ description: AOP in 100 lines of ruby
63
+ email: fonsan@gmail.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files:
67
+ - LICENSE.txt
68
+ - README.rdoc
69
+ files:
70
+ - .document
71
+ - .rspec
72
+ - .rvmrc
73
+ - Gemfile
74
+ - Gemfile.lock
75
+ - LICENSE.txt
76
+ - README.rdoc
77
+ - Rakefile
78
+ - VERSION
79
+ - examples/everything.rb
80
+ - lib/tivoli.rb
81
+ - spec/spec_helper.rb
82
+ - spec/tivoli_spec.rb
83
+ - tivoli.gemspec
84
+ homepage: http://github.com/Fonsan/tivoli
85
+ licenses:
86
+ - MIT
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ segments:
98
+ - 0
99
+ hash: -616944341335215158
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 1.8.21
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: AOP in 100 lines of ruby
112
+ test_files: []