enhanced_hooks 0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/.rvmrc +1 -0
- data/Gemfile +7 -0
- data/LICENSE +20 -0
- data/LICENSE.txt +22 -0
- data/README.rdoc +73 -0
- data/Rakefile +1 -0
- data/enhanced_hooks.gemspec +19 -0
- data/lib/enhanced_hooks.rb +15 -0
- data/lib/enhanced_hooks/engine.rb +21 -0
- data/lib/enhanced_hooks/hooks.rb +116 -0
- data/lib/enhanced_hooks/version.rb +3 -0
- data/lib/generators/enhanced_hooks/enhanced_hooks_generator.rb +28 -0
- data/lib/generators/enhanced_hooks/templates/event.rb +11 -0
- data/lib/generators/enhanced_hooks/templates/functional_test.rb +27 -0
- data/test/helper.rb +10 -0
- data/test/test_event_machine.rb +7 -0
- metadata +64 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use ruby-1.9.3-p362@event_machine
|
data/Gemfile
ADDED
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/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 readline
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
= enhanced_hooks
|
2
|
+
With enhanced hooks, 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 on fly.
|
13
|
+
|
14
|
+
Usage:
|
15
|
+
|
16
|
+
in your Gemfile:
|
17
|
+
|
18
|
+
gem "enhanced_hooks", :version => ">=0.4.1"
|
19
|
+
|
20
|
+
and the generate command :
|
21
|
+
|
22
|
+
~your_project_path> rails g enhanced_hooks create_favorite Favorites create
|
23
|
+
|
24
|
+
here 'create_event' stands for the event name, and 'Favorites' 'create' for which contorller#action to observe
|
25
|
+
|
26
|
+
then it will generate a event file named create_favorite_event.rb under the Rails.root/events directory, which content like following:
|
27
|
+
|
28
|
+
|
29
|
+
for_action FavoritesController, :create do
|
30
|
+
|
31
|
+
before do
|
32
|
+
# This will be called before FavoriteController#create
|
33
|
+
end
|
34
|
+
|
35
|
+
after do
|
36
|
+
# This will be called after FavoriteController#create
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
now you can add your code in the block and after block, enjoy.
|
43
|
+
|
44
|
+
= to observe multiple actions
|
45
|
+
|
46
|
+
to observe multiple actions, you should:
|
47
|
+
|
48
|
+
for_actions [[FavoritesController, :create], [BlogsController,:create]] do
|
49
|
+
|
50
|
+
before do
|
51
|
+
# your code goes here
|
52
|
+
end
|
53
|
+
|
54
|
+
after do
|
55
|
+
# your code goes here
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
== Note on Patches/Pull Requests
|
62
|
+
|
63
|
+
* Fork the project.
|
64
|
+
* Make your feature addition or bug fix.
|
65
|
+
* Add tests for it. This is important so I don't break it in a
|
66
|
+
future version unintentionally.
|
67
|
+
* Commit, do not mess with rakefile, version, or history.
|
68
|
+
(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)
|
69
|
+
* Send me a pull request. Bonus points for topic branches.
|
70
|
+
|
71
|
+
== Copyright
|
72
|
+
|
73
|
+
Copyright (c) 2010 tim.teng. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'enhanced_hooks/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "enhanced_hooks"
|
8
|
+
gem.version = EnhancedHooks::VERSION
|
9
|
+
gem.authors = ["readline"]
|
10
|
+
gem.email = ["tim.rubist@gmail.com"]
|
11
|
+
gem.description = %q{enhanced before/after filter hooks}
|
12
|
+
gem.summary = %q{enhanced before/after filter hooks}
|
13
|
+
gem.homepage = "http://github.com/tteng/enhanced_hooks"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module EnhancedHooks
|
2
|
+
class Engine < Rails::Engine
|
3
|
+
|
4
|
+
initializer "enhanced_hooks.load_app_instance_data" do |app|
|
5
|
+
EnhancedHooks.setup do |config|
|
6
|
+
config.app_root = app.root
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
#initialize "enhanced_hooks.load_static_assets" do |app|
|
11
|
+
# app.middleware.use ::ActionDispatc::Static, "#{root}/public"
|
12
|
+
#end
|
13
|
+
|
14
|
+
initializer "enhanced_hooks.application" do
|
15
|
+
ActiveSupport.on_load :action_controller do
|
16
|
+
include EnhancedHooks::Hooks
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
require "pp"
|
2
|
+
require 'benchmark'
|
3
|
+
|
4
|
+
module EnhancedHooks
|
5
|
+
|
6
|
+
module Hooks
|
7
|
+
|
8
|
+
EVENTS_DIR = "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,28 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
class EnhancedHooksGenerator < Rails::Generators::Base
|
3
|
+
source_root File.expand_path('../templates', __FILE__)
|
4
|
+
|
5
|
+
argument :this_event_name, :type => :string
|
6
|
+
argument :controller_name, :type => :string
|
7
|
+
argument :action_name, :type => :string
|
8
|
+
|
9
|
+
def generate_event
|
10
|
+
template "event.rb", "#{EnhancedHooks::Hooks::EVENTS_DIR}/#{parsed_event_name}_event.rb"
|
11
|
+
template "functional_test.rb", "test/functional/events/#{parsed_event_name}_test.rb"
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def parsed_event_name
|
17
|
+
this_event_name.underscore
|
18
|
+
end
|
19
|
+
|
20
|
+
def parsed_controller_name
|
21
|
+
controller_name.camelize
|
22
|
+
end
|
23
|
+
|
24
|
+
def parsed_action_name
|
25
|
+
action_name.underscore
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
for_action <%= parsed_controller_name %>Controller, :<%= action_name %> do
|
2
|
+
|
3
|
+
before do
|
4
|
+
# This will be called before <%= parsed_controller_name %>#<%= parsed_action_name %>
|
5
|
+
end
|
6
|
+
|
7
|
+
after do
|
8
|
+
# This will be called after <%= parsed_controller_name %>#<%= parsed_action_name %>
|
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 '<%= parsed_controller_name.underscore -%>_controller'
|
4
|
+
|
5
|
+
# Re-raise errors caught by the controller.
|
6
|
+
class <%= parsed_controller_name -%>; def rescue_action(e) raise e end; end
|
7
|
+
|
8
|
+
class <%= parsed_event_name.camelize -%>Test < EventBaseTest
|
9
|
+
|
10
|
+
fixtures :users
|
11
|
+
|
12
|
+
def setup
|
13
|
+
@controller = <%= parsed_controller_name.camelize-%>.new
|
14
|
+
@request = ActionController::TestRequest.new
|
15
|
+
@response = ActionController::TestResponse.new
|
16
|
+
|
17
|
+
prepare
|
18
|
+
load_event "app/events/<%= parsed_event_name-%>_event.rb"
|
19
|
+
emulate_login
|
20
|
+
end
|
21
|
+
|
22
|
+
# Put actual tests here
|
23
|
+
def test_true
|
24
|
+
assert true
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/test/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enhanced_hooks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.5'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- readline
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-09 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: enhanced before/after filter hooks
|
15
|
+
email:
|
16
|
+
- tim.rubist@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- .rvmrc
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE
|
25
|
+
- LICENSE.txt
|
26
|
+
- README.rdoc
|
27
|
+
- Rakefile
|
28
|
+
- enhanced_hooks.gemspec
|
29
|
+
- lib/enhanced_hooks.rb
|
30
|
+
- lib/enhanced_hooks/engine.rb
|
31
|
+
- lib/enhanced_hooks/hooks.rb
|
32
|
+
- lib/enhanced_hooks/version.rb
|
33
|
+
- lib/generators/enhanced_hooks/enhanced_hooks_generator.rb
|
34
|
+
- lib/generators/enhanced_hooks/templates/event.rb
|
35
|
+
- lib/generators/enhanced_hooks/templates/functional_test.rb
|
36
|
+
- test/helper.rb
|
37
|
+
- test/test_event_machine.rb
|
38
|
+
homepage: http://github.com/tteng/enhanced_hooks
|
39
|
+
licenses: []
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.8.24
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: enhanced before/after filter hooks
|
62
|
+
test_files:
|
63
|
+
- test/helper.rb
|
64
|
+
- test/test_event_machine.rb
|