event_machine 0.2.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
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 tim.teng
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,87 @@
1
+ = event_machine
2
+ With event machine, you can keep an eye on any action on your rails controller, say in your sns website,
3
+ when user posts a blog, you need to notice all of his friends, but it's really urgly to do like this:
4
+
5
+ <pre>
6
+
7
+ BlogController:
8
+
9
+ def create
10
+ # new and save code
11
+ notice_all_friends
12
+ end
13
+
14
+ end
15
+
16
+ </pre>
17
+
18
+ let's take another scenario, in your sns site, when one user makes friends with the other one, we should notice
19
+ all friends of these two people, but how to?
20
+ we'd beeter have an observer on the action, the code in the make_friends action block only concerns about building
21
+ relationship between the two people, the observer then notice other people, and this is also what IoC(AOP) teach us.
22
+
23
+ Usage:
24
+
25
+ in your config/environment.rb:
26
+
27
+ config.gem "event_machine", :version => ">=0.2.0"
28
+
29
+ and the generate command :
30
+
31
+ <pre>
32
+ ~your_project_path> script/generate event_machine create_favorite FavoriteController create
33
+ </pre>
34
+
35
+ then it will generate a event file which content like following:
36
+
37
+ <pre>
38
+
39
+ for_action FavoritesController, :create do
40
+
41
+ before do
42
+ # This will be called before FavoriteController#create
43
+ end
44
+
45
+ after do
46
+ # This will be called after FavoriteController#create
47
+ end
48
+
49
+ end
50
+
51
+ </pre>
52
+
53
+ now you can add your code in the block and after block, enjoy.
54
+
55
+ = to observe multiple actions
56
+
57
+ to observe multiple actions, you should:
58
+
59
+ <pre>
60
+ for_actions [[FavoritesController, :create], [BlogsController,:create]] do
61
+
62
+ before do
63
+ # your code goes here
64
+ end
65
+
66
+ after do
67
+ # your code goes here
68
+ end
69
+
70
+ end
71
+
72
+ </pre>
73
+
74
+
75
+ == Note on Patches/Pull Requests
76
+
77
+ * Fork the project.
78
+ * Make your feature addition or bug fix.
79
+ * Add tests for it. This is important so I don't break it in a
80
+ future version unintentionally.
81
+ * Commit, do not mess with rakefile, version, or history.
82
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
83
+ * Send me a pull request. Bonus points for topic branches.
84
+
85
+ == Copyright
86
+
87
+ Copyright (c) 2010 tim.teng. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "event_machine"
8
+ gem.summary = %Q{an observer to record events}
9
+ gem.description = %Q{Event Machine is observer to record any action you want to keep an eye on}
10
+ gem.email = "tim.rubist@gmail.com"
11
+ gem.homepage = "http://github.com/tteng/event_machine"
12
+ gem.authors = ["tim.teng"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "event_machine #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
@@ -0,0 +1,58 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{event_machine}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["tim.teng"]
12
+ s.date = %q{2010-05-12}
13
+ s.description = %q{Event Machine is observer to record any action you want to keep an eye on}
14
+ s.email = %q{tim.rubist@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "event_machine.gemspec",
27
+ "generators/event_machine/USAGE",
28
+ "generators/event_machine/event_machine_generator.rb",
29
+ "generators/event_machine/templates/event.rb",
30
+ "generators/event_machine/templates/functional_test.rb",
31
+ "lib/event_machine.rb",
32
+ "test/helper.rb",
33
+ "test/test_event_machine.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/tteng/event_machine}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.6}
39
+ s.summary = %q{an observer to record events}
40
+ s.test_files = [
41
+ "test/test_event_machine.rb",
42
+ "test/helper.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
56
+ end
57
+ end
58
+
@@ -0,0 +1,2 @@
1
+ Usage: ./script/generate event EventName Controller Action
2
+ Example: ./script/generate event media_rated Admin::ContentController rate_media
@@ -0,0 +1,43 @@
1
+ class EventMachineGenerator < Rails::Generator::NamedBase
2
+
3
+ def initialize(runtime_args, runtime_options = {})
4
+ super
5
+ @event_name = runtime_args.shift
6
+ @controller_name = runtime_args.shift
7
+ @controller_action = runtime_args.shift
8
+ unless valid_options?
9
+ puts "Invalid arguments!"
10
+ puts banner
11
+ exit
12
+ end
13
+ end
14
+
15
+ def manifest
16
+ record do |m|
17
+ m.directory "app/events"
18
+ m.directory "test/functional/events"
19
+ m.template "event.rb",
20
+ File.join('app/events', "#{@event_name}Event".underscore + ".rb"),
21
+ :assigns => { :controller => @controller_name, :action => @controller_action }
22
+
23
+ m.template "functional_test.rb",
24
+ File.join('test/functional/events', "#{@event_name}Event".underscore + "_test.rb"),
25
+ :assigns => { :controller => @controller_name,
26
+ :action => @controller_action,
27
+ :event => "#{@event_name}Event"
28
+ }
29
+ end
30
+ end
31
+
32
+ def banner
33
+ "Usage: #{$0} event EventName Controller Action"
34
+ "Example: #{$0} event media_rated Admin::ContentController rate"
35
+ end
36
+
37
+ private
38
+
39
+ def valid_options?
40
+ [@event_name, @controller_name, @controller_action].all? { |v| !v.blank? }
41
+ end
42
+
43
+ end
@@ -0,0 +1,11 @@
1
+ for_action <%= controller %>, :<%= action %> do
2
+
3
+ before do
4
+ # This will be called before <%= controller %>#<%= action %>
5
+ end
6
+
7
+ after do
8
+ # This will be called after <%= controller %>#<%= action %>
9
+ end
10
+
11
+ end
@@ -0,0 +1,27 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+ require File.dirname(__FILE__) + '/../../functional/events/events_base_test'
3
+ require '<%= controller.underscore -%>'
4
+
5
+ # Re-raise errors caught by the controller.
6
+ class <%= controller.classify -%>; def rescue_action(e) raise e end; end
7
+
8
+ class <%= event.classify -%>Test < EventBaseTest
9
+
10
+ fixtures :users
11
+
12
+ def setup
13
+ @controller = <%= controller.classify -%>.new
14
+ @request = ActionController::TestRequest.new
15
+ @response = ActionController::TestResponse.new
16
+
17
+ prepare
18
+ load_event "#{EVENTS_DIR}/<%= event.underscore -%>.rb"
19
+ emulate_login
20
+ end
21
+
22
+ # Put actual tests here
23
+ def test_true
24
+ assert true
25
+ end
26
+
27
+ end
@@ -0,0 +1,112 @@
1
+ require "pp"
2
+ require 'benchmark'
3
+
4
+ module EventMachine
5
+
6
+ EVENTS_DIR = (defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/app/events" : "app/events")
7
+
8
+ def self.included(base)
9
+ class_eval <<-EOF
10
+ @@reporter_events = Array.new
11
+ EOF
12
+ base.class_eval <<-BCE
13
+ around_filter :reporter_around_filter
14
+ BCE
15
+ end
16
+
17
+ protected
18
+
19
+ def reporter_around_filter
20
+
21
+ events_load_bm = Benchmark.measure do
22
+ load_events if @@reporter_events.empty?
23
+ end
24
+
25
+ events = @@reporter_events.select {|e| e.match_event?(self.class, action_name)}
26
+
27
+ # Calling all before actions for this controler and action
28
+ events.each { |event| event.call_before(self) }
29
+
30
+ # Calling original action
31
+ yield
32
+
33
+ # Calling all after actions for this controler and action
34
+ events.each { |event| event.call_after(self) }
35
+ end
36
+
37
+ def for_action(controller, action, &block)
38
+ @@reporter_events << EventMeta.new(controller, action, &block)
39
+ end
40
+
41
+ def for_actions(actions_array, &block)
42
+ actions_array.each do |item|
43
+ @@reporter_events << EventMeta.new(item[0], item[1], &block)
44
+ end
45
+ end
46
+
47
+ def turn_off_for_action(controller, action, &block)
48
+ for_action(controller, action, &block) if test?
49
+ end
50
+
51
+ def load_events
52
+ return if test?
53
+ Dir["#{EVENTS_DIR}/**/*event.rb"].collect do |file|
54
+ load_event(file)
55
+ end
56
+ end
57
+
58
+ def load_event(file)
59
+ clear_events if test?
60
+ code = File.new(file).readlines.join('')
61
+ begin
62
+ instance_eval code, __FILE__, __LINE__
63
+ rescue => e
64
+ logger.warn "FAILED!"
65
+ logger.warn e.backtrace
66
+ end
67
+ end
68
+
69
+ def clear_events
70
+ @@reporter_events.clear
71
+ end
72
+
73
+ def reporter_events
74
+ @@reporter_events
75
+ end
76
+
77
+ def test?
78
+ ENV["RAILS_ENV"] == "test"
79
+ end
80
+
81
+ class EventMeta
82
+
83
+ def initialize(controller_class, name, &block)
84
+ @controller_class = controller_class
85
+ @action = name
86
+ self.instance_eval(&block)
87
+ end
88
+
89
+ def call_before(controller_instance)
90
+ controller_instance.instance_eval(&@before) if @before
91
+ end
92
+
93
+ def call_after(controller_instance)
94
+ controller_instance.instance_eval(&@after) if @after
95
+ end
96
+
97
+ def match_event?(clazz, action_name)
98
+ @controller_class == clazz && @action.to_s == action_name.to_s
99
+ end
100
+ private
101
+
102
+ def before(&block)
103
+ @before = block
104
+ end
105
+
106
+ def after(&block)
107
+ @after = block
108
+ end
109
+
110
+ end
111
+
112
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'event_machine'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestEventMachine < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: event_machine
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
+ platform: ruby
11
+ authors:
12
+ - tim.teng
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-12 00:00:00 +08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: thoughtbot-shoulda
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :development
31
+ version_requirements: *id001
32
+ description: Event Machine is observer to record any action you want to keep an eye on
33
+ email: tim.rubist@gmail.com
34
+ executables: []
35
+
36
+ extensions: []
37
+
38
+ extra_rdoc_files:
39
+ - LICENSE
40
+ - README.rdoc
41
+ files:
42
+ - .document
43
+ - .gitignore
44
+ - LICENSE
45
+ - README.rdoc
46
+ - Rakefile
47
+ - VERSION
48
+ - event_machine.gemspec
49
+ - generators/event_machine/USAGE
50
+ - generators/event_machine/event_machine_generator.rb
51
+ - generators/event_machine/templates/event.rb
52
+ - generators/event_machine/templates/functional_test.rb
53
+ - lib/event_machine.rb
54
+ - test/helper.rb
55
+ - test/test_event_machine.rb
56
+ has_rdoc: true
57
+ homepage: http://github.com/tteng/event_machine
58
+ licenses: []
59
+
60
+ post_install_message:
61
+ rdoc_options:
62
+ - --charset=UTF-8
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.3.6
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: an observer to record events
86
+ test_files:
87
+ - test/test_event_machine.rb
88
+ - test/helper.rb