Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1, migrate to rails3 style gem and generator
- Loading branch information
Showing
7 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rvm use ruby-1.9.3-p362@event_machine |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module EventMachine | ||
class Engine < Rails::Engine | ||
|
||
initializer "event_machine.load_app_instance_data" do |app| | ||
EventMachine.setup do |config| | ||
config.app_root = app.root | ||
end | ||
end | ||
|
||
#initialize "event_machine.load_static_assets" do |app| | ||
# app.middleware.use ::ActionDispatc::Static, "#{root}/public" | ||
#end | ||
|
||
initializer "event_machine.application" do | ||
ActiveSupport.on_load :action_controller do | ||
include EventMachine::Hooks | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
require "pp" | ||
require 'benchmark' | ||
|
||
module EventMachine | ||
|
||
module Hooks | ||
|
||
EVENTS_DIR = "app/events" | ||
|
||
def self.included(base) | ||
class_eval <<-EOF | ||
@@reporter_events = Array.new | ||
EOF | ||
base.class_eval <<-BCE | ||
around_filter :reporter_around_filter | ||
BCE | ||
end | ||
|
||
protected | ||
|
||
def reporter_around_filter | ||
|
||
events_load_bm = Benchmark.measure do | ||
load_events if @@reporter_events.empty? | ||
end | ||
|
||
events = @@reporter_events.select {|e| e.match_event?(self.class, action_name)} | ||
|
||
# Calling all before actions for this controler and action | ||
events.each { |event| event.call_before(self) } | ||
|
||
# Calling original action | ||
yield | ||
|
||
# Calling all after actions for this controler and action | ||
events.each { |event| event.call_after(self) } | ||
end | ||
|
||
def for_action(controller, action, &block) | ||
@@reporter_events << EventMeta.new(controller, action, &block) | ||
end | ||
|
||
def for_actions(actions_array, &block) | ||
actions_array.each do |item| | ||
@@reporter_events << EventMeta.new(item[0], item[1], &block) | ||
end | ||
end | ||
|
||
def turn_off_for_action(controller, action, &block) | ||
for_action(controller, action, &block) if test? | ||
end | ||
|
||
def load_events | ||
return if test? | ||
Dir["#{Rails.root + EVENTS_DIR}/**/*event.rb"].collect do |file| | ||
load_event(file) | ||
end | ||
end | ||
|
||
def load_event(file) | ||
clear_events if test? | ||
code = File.new(file).readlines.join('') | ||
begin | ||
instance_eval code, __FILE__, __LINE__ | ||
rescue => e | ||
logger.warn "FAILED!" | ||
logger.warn e.backtrace | ||
end | ||
end | ||
|
||
def clear_events | ||
@@reporter_events.clear | ||
end | ||
|
||
def reporter_events | ||
@@reporter_events | ||
end | ||
|
||
def test? | ||
ENV["RAILS_ENV"] == "test" | ||
end | ||
|
||
class EventMeta | ||
|
||
def initialize(controller_class, name, &block) | ||
@controller_class = controller_class | ||
@action = name | ||
self.instance_eval(&block) | ||
end | ||
|
||
def call_before(controller_instance) | ||
controller_instance.instance_eval(&@before) if @before | ||
end | ||
|
||
def call_after(controller_instance) | ||
controller_instance.instance_eval(&@after) if @after | ||
end | ||
|
||
def match_event?(clazz, action_name) | ||
@controller_class == clazz && @action.to_s == action_name.to_s | ||
end | ||
private | ||
|
||
def before(&block) | ||
@before = block | ||
end | ||
|
||
def after(&block) | ||
@after = block | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module EventMachine | ||
VERSION = "0.4.0" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'rails/generators' | ||
class EventMachineGenerator < Rails::Generators::Base | ||
source_root File.expand_path('../templates', __FILE__) | ||
|
||
argument :this_event_name, :type => :string | ||
argument :controller_name, :type => :string | ||
argument :action_name, :type => :string | ||
|
||
def generate_event | ||
template "event.rb", "app/events/#{parsed_event_name}_event.rb" | ||
template "functional_test.rb", "test/functional/events/#{parsed_event_name}_test.rb" | ||
end | ||
|
||
private | ||
|
||
def parsed_event_name | ||
this_event_name.underscore | ||
end | ||
|
||
def parsed_controller_name | ||
controller_name.camelize | ||
end | ||
|
||
def parsed_action_name | ||
action_name.underscore | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
for_action <%= parsed_controller_name %>Controller, :<%= action_name %> do | ||
|
||
before do | ||
# This will be called before <%= parsed_controller_name %>#<%= parsed_action_name %> | ||
end | ||
|
||
after do | ||
# This will be called after <%= parsed_controller_name %>#<%= parsed_action_name %> | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require File.dirname(__FILE__) + '/../../test_helper' | ||
require File.dirname(__FILE__) + '/../../functional/events/events_base_test' | ||
require '<%= parsed_controller_name.underscore -%>_controller' | ||
|
||
# Re-raise errors caught by the controller. | ||
class <%= parsed_controller_name -%>; def rescue_action(e) raise e end; end | ||
class <%= parsed_event_name.camelize -%>Test < EventBaseTest | ||
|
||
fixtures :users | ||
|
||
def setup | ||
@controller = <%= parsed_controller_name.camelize-%>.new | ||
@request = ActionController::TestRequest.new | ||
@response = ActionController::TestResponse.new | ||
prepare | ||
load_event "app/events/<%= parsed_event_name-%>_event.rb" | ||
emulate_login | ||
end | ||
|
||
# Put actual tests here | ||
def test_true | ||
assert true | ||
end | ||
|
||
end |