event_machine 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/event_machine.rb CHANGED
@@ -1,112 +1,13 @@
1
- require "pp"
2
- require 'benchmark'
3
-
4
- module EventMachine
5
-
6
- EVENTS_DIR = "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
1
+ require "event_machine/version"
2
+ require "active_support/dependencies"
3
+
4
+ module EventMachine
5
+ mattr_accessor :app_root
6
+
7
+ def self.setup
8
+ yield self
9
+ end
10
+ end
11
+
12
+ require "event_machine/engine"
13
+ require "event_machine/hooks"
@@ -0,0 +1,21 @@
1
+ module EventMachine
2
+ class Engine < Rails::Engine
3
+
4
+ initializer "event_machine.load_app_instance_data" do |app|
5
+ EventMachine.setup do |config|
6
+ config.app_root = app.root
7
+ end
8
+ end
9
+
10
+ #initialize "event_machine.load_static_assets" do |app|
11
+ # app.middleware.use ::ActionDispatc::Static, "#{root}/public"
12
+ #end
13
+
14
+ initializer "event_machine.application" do
15
+ ActiveSupport.on_load :action_controller do
16
+ include EventMachine::Hooks
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,116 @@
1
+ require "pp"
2
+ require 'benchmark'
3
+
4
+ module EventMachine
5
+
6
+ module Hooks
7
+
8
+ EVENTS_DIR = "app/events"
9
+
10
+ def self.included(base)
11
+ class_eval <<-EOF
12
+ @@reporter_events = Array.new
13
+ EOF
14
+ base.class_eval <<-BCE
15
+ around_filter :reporter_around_filter
16
+ BCE
17
+ end
18
+
19
+ protected
20
+
21
+ def reporter_around_filter
22
+
23
+ events_load_bm = Benchmark.measure do
24
+ load_events if @@reporter_events.empty?
25
+ end
26
+
27
+ events = @@reporter_events.select {|e| e.match_event?(self.class, action_name)}
28
+
29
+ # Calling all before actions for this controler and action
30
+ events.each { |event| event.call_before(self) }
31
+
32
+ # Calling original action
33
+ yield
34
+
35
+ # Calling all after actions for this controler and action
36
+ events.each { |event| event.call_after(self) }
37
+ end
38
+
39
+ def for_action(controller, action, &block)
40
+ @@reporter_events << EventMeta.new(controller, action, &block)
41
+ end
42
+
43
+ def for_actions(actions_array, &block)
44
+ actions_array.each do |item|
45
+ @@reporter_events << EventMeta.new(item[0], item[1], &block)
46
+ end
47
+ end
48
+
49
+ def turn_off_for_action(controller, action, &block)
50
+ for_action(controller, action, &block) if test?
51
+ end
52
+
53
+ def load_events
54
+ return if test?
55
+ Dir["#{Rails.root + EVENTS_DIR}/**/*event.rb"].collect do |file|
56
+ load_event(file)
57
+ end
58
+ end
59
+
60
+ def load_event(file)
61
+ clear_events if test?
62
+ code = File.new(file).readlines.join('')
63
+ begin
64
+ instance_eval code, __FILE__, __LINE__
65
+ rescue => e
66
+ logger.warn "FAILED!"
67
+ logger.warn e.backtrace
68
+ end
69
+ end
70
+
71
+ def clear_events
72
+ @@reporter_events.clear
73
+ end
74
+
75
+ def reporter_events
76
+ @@reporter_events
77
+ end
78
+
79
+ def test?
80
+ ENV["RAILS_ENV"] == "test"
81
+ end
82
+
83
+ class EventMeta
84
+
85
+ def initialize(controller_class, name, &block)
86
+ @controller_class = controller_class
87
+ @action = name
88
+ self.instance_eval(&block)
89
+ end
90
+
91
+ def call_before(controller_instance)
92
+ controller_instance.instance_eval(&@before) if @before
93
+ end
94
+
95
+ def call_after(controller_instance)
96
+ controller_instance.instance_eval(&@after) if @after
97
+ end
98
+
99
+ def match_event?(clazz, action_name)
100
+ @controller_class == clazz && @action.to_s == action_name.to_s
101
+ end
102
+ private
103
+
104
+ def before(&block)
105
+ @before = block
106
+ end
107
+
108
+ def after(&block)
109
+ @after = block
110
+ end
111
+
112
+ end
113
+
114
+ end
115
+
116
+ end
@@ -0,0 +1,3 @@
1
+ module EventMachine
2
+ VERSION = "0.4.0"
3
+ end
@@ -1,16 +1,13 @@
1
- #coding: utf-8
2
- require 'fileutils'
3
-
1
+ require 'rails/generators'
4
2
  class EventMachineGenerator < Rails::Generators::Base
5
-
6
3
  source_root File.expand_path('../templates', __FILE__)
7
4
 
8
5
  argument :this_event_name, :type => :string
9
6
  argument :controller_name, :type => :string
10
- argument :action_name, :type => :string
7
+ argument :action_name, :type => :string
11
8
 
12
9
  def generate_event
13
- template "event.rb", "events/#{parsed_event_name}_event.rb"
10
+ template "event.rb", "app/events/#{parsed_event_name}_event.rb"
14
11
  template "functional_test.rb", "test/functional/events/#{parsed_event_name}_test.rb"
15
12
  end
16
13
 
metadata CHANGED
@@ -1,50 +1,63 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: event_machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
- - tim.teng
8
+ - tteng
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-16 00:00:00.000000000Z
12
+ date: 2013-01-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: thoughtbot-shoulda
16
- requirement: &70202617010360 !ruby/object:Gem::Requirement
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :development
21
+ version: 3.0.0
22
+ type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70202617010360
25
- description: Event Machine is observer to record any action you want to keep an eye
26
- on
27
- email: tim.rubist@gmail.com
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rails
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 3.0.0
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: 3.0.0
46
+ description: simple elegant customize hooks
47
+ email:
48
+ - tim.rubist@gmail.com
28
49
  executables: []
29
50
  extensions: []
30
- extra_rdoc_files:
31
- - LICENSE
32
- - README.rdoc
51
+ extra_rdoc_files: []
33
52
  files:
34
- - .document
35
- - LICENSE
36
- - README.rdoc
37
- - Rakefile
38
- - VERSION
39
- - event_machine.gemspec
40
- - generators/event_machine/USAGE
41
- - generators/event_machine/event_machine_generator.rb
42
- - generators/event_machine/templates/event.rb
43
- - generators/event_machine/templates/functional_test.rb
44
53
  - lib/event_machine.rb
45
- - test/helper.rb
46
- - test/test_event_machine.rb
47
- homepage: http://github.com/tteng/event_machine
54
+ - lib/event_machine/engine.rb
55
+ - lib/event_machine/hooks.rb
56
+ - lib/event_machine/version.rb
57
+ - lib/generators/event_machine/event_machine_generator.rb
58
+ - lib/generators/event_machine/templates/event.rb
59
+ - lib/generators/event_machine/templates/functional_test.rb
60
+ homepage: http://github.com/tteng
48
61
  licenses: []
49
62
  post_install_message:
50
63
  rdoc_options: []
@@ -64,8 +77,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
77
  version: '0'
65
78
  requirements: []
66
79
  rubyforge_project:
67
- rubygems_version: 1.8.10
80
+ rubygems_version: 1.8.24
68
81
  signing_key:
69
82
  specification_version: 3
70
- summary: an observer to record events
83
+ summary: simple elegant around filters to handle routines
71
84
  test_files: []
data/.document DELETED
@@ -1,5 +0,0 @@
1
- README.rdoc
2
- lib/**/*.rb
3
- bin/*
4
- features/**/*.feature
5
- LICENSE
data/LICENSE DELETED
@@ -1,20 +0,0 @@
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 DELETED
@@ -1,77 +0,0 @@
1
- = event_machine
2
- With event machine, you can keep an eye on any action on your rails controller, say in your sns website, when user posts a blog, you need to notice all of his friends, but it's really urgly to do like this:
3
-
4
- BlogController:
5
- def create
6
- # new and save code
7
- notice_all_friends
8
- end
9
- end
10
-
11
- let's take another scenario, in your sns site, when one user makes friends with the other one, we should notice all friends of these two people, but how to?
12
- we'd beeter have an observer on the action, the code in the make_friends action block only concerns about building relationship between the two people, the observer then notice other people, and this is also what IoC(AOP) teach us.
13
-
14
- Usage:
15
-
16
- in your config/environment.rb:
17
-
18
- gem "event_machine", :version => ">=0.2.1"
19
-
20
- application_controller.rb:
21
-
22
- class ApplicationController < ActionController::Base
23
- include EventMachine
24
- end
25
-
26
- and the generate command :
27
-
28
- ~your_project_path> rails g event_machine create_favorite FavoriteController create
29
-
30
- then it will generate a event file which content like following:
31
-
32
-
33
- for_action FavoritesController, :create do
34
-
35
- before do
36
- # This will be called before FavoriteController#create
37
- end
38
-
39
- after do
40
- # This will be called after FavoriteController#create
41
- end
42
-
43
- end
44
-
45
-
46
- now you can add your code in the block and after block, enjoy.
47
-
48
- = to observe multiple actions
49
-
50
- to observe multiple actions, you should:
51
-
52
- for_actions [[FavoritesController, :create], [BlogsController,:create]] do
53
-
54
- before do
55
- # your code goes here
56
- end
57
-
58
- after do
59
- # your code goes here
60
- end
61
-
62
- end
63
-
64
-
65
- == Note on Patches/Pull Requests
66
-
67
- * Fork the project.
68
- * Make your feature addition or bug fix.
69
- * Add tests for it. This is important so I don't break it in a
70
- future version unintentionally.
71
- * Commit, do not mess with rakefile, version, or history.
72
- (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)
73
- * Send me a pull request. Bonus points for topic branches.
74
-
75
- == Copyright
76
-
77
- Copyright (c) 2010 tim.teng. See LICENSE for details.
data/Rakefile DELETED
@@ -1,53 +0,0 @@
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 DELETED
@@ -1 +0,0 @@
1
- 0.3.1
@@ -1,51 +0,0 @@
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 = "event_machine"
8
- s.version = "0.3.1"
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 = "2011-12-16"
13
- s.description = "Event Machine is observer to record any action you want to keep an eye on"
14
- s.email = "tim.rubist@gmail.com"
15
- s.extra_rdoc_files = [
16
- "LICENSE",
17
- "README.rdoc"
18
- ]
19
- s.files = [
20
- ".document",
21
- "LICENSE",
22
- "README.rdoc",
23
- "Rakefile",
24
- "VERSION",
25
- "event_machine.gemspec",
26
- "generators/event_machine/USAGE",
27
- "generators/event_machine/event_machine_generator.rb",
28
- "generators/event_machine/templates/event.rb",
29
- "generators/event_machine/templates/functional_test.rb",
30
- "lib/event_machine.rb",
31
- "test/helper.rb",
32
- "test/test_event_machine.rb"
33
- ]
34
- s.homepage = "http://github.com/tteng/event_machine"
35
- s.require_paths = ["lib"]
36
- s.rubygems_version = "1.8.10"
37
- s.summary = "an observer to record events"
38
-
39
- if s.respond_to? :specification_version then
40
- s.specification_version = 3
41
-
42
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
- s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
44
- else
45
- s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
46
- end
47
- else
48
- s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
49
- end
50
- end
51
-
@@ -1,2 +0,0 @@
1
- Usage: rails g event_machine EventName Controller Action
2
- Example: rails g event_machine media_rated Admin::ContentController rate_media
data/test/helper.rb DELETED
@@ -1,10 +0,0 @@
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
@@ -1,7 +0,0 @@
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