Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Automatically load all listeners

  • Loading branch information...
commit 77c81834555840671892fbf900e9b7389388b937 1 parent 4626c59
Joe Ferris and Mike Burns authored jferris committed
View
1  lib/turning.rb
@@ -4,3 +4,4 @@
require 'turning/renderer'
require 'turning/static_cascade'
require 'turning/railtie'
+require 'turning/listener_loader'
View
11 lib/turning/listener_loader.rb
@@ -0,0 +1,11 @@
+module Turning
+ class ListenerLoader
+ def self.load
+ Dir.glob(Rails.root.join('app', 'listeners', '**', '*.rb')).each do |listener_file|
+ Kernel.load(listener_file)
+ listener_class = File.basename(listener_file).gsub('.rb', '').classify.constantize
+ listener_class.new.listen
+ end
+ end
+ end
+end
View
5 lib/turning/railtie.rb
@@ -2,11 +2,16 @@
require 'turning/static_cascade'
require 'turning/file_storage'
require 'turning/configuration'
+require 'turning/listener_loader'
module Turning
class Railtie < ::Rails::Railtie
config.turning = Configuration.new
+ config.to_prepare do
+ ListenerLoader.load
+ end
+
initializer('turning.middleware') do
config.app_middleware.use StaticCascade, config.turning.storage
end
View
43 spec/turning/integration_spec.rb
@@ -5,40 +5,33 @@
include Rack::Test::Methods
it 'listens to events and renders the view to a static file' do
- listener_class = Class.new(Turning::Listener) do
- def initialize(model)
- super()
- @model = model
- end
-
- def self.name
- 'ExamplesListener'
- end
-
- def listen
- @model.on_update do
- render 'index', '/', greeting: 'Hello'
+ create_file('app/listeners/examples_listener.rb', <<-RUBY)
+ class ExamplesListener < Turning::Listener
+ def listen
+ Example.on_update do
+ render 'index', '/', greeting: 'Hello'
+ end
end
end
- end
+ RUBY
- model_class = Class.new do
- def on_update(&block)
- @on_update = block
- end
+ create_file('app/models/example.rb', <<-RUBY)
+ class Example
+ def self.on_update(&block)
+ @on_update = block
+ end
- def trigger_update
- @on_update.call
+ def self.trigger_update
+ @on_update.call
+ end
end
- end
+ RUBY
create_view('examples/index.html.erb', '<%= @greeting %>')
- model = model_class.new
- listener = listener_class.new(model)
+ Turning::ListenerLoader.load
- listener.listen
- model.trigger_update
+ Example.trigger_update
get '/'
last_response.body.should == 'Hello'
View
22 spec/turning/listener_loader_spec.rb
@@ -0,0 +1,22 @@
+require 'spec_helper'
+require 'turning/listener_loader'
+
+describe Turning::ListenerLoader do
+ it 'loads listeners' do
+ create_file('app/listeners/examples_listener.rb', <<-RUBY)
+ class ExamplesListener
+ def self.listening?
+ defined?(@@listening) && @@listening
+ end
+
+ def listen
+ @@listening = true
+ end
+ end
+ RUBY
+
+ Turning::ListenerLoader.load
+
+ ExamplesListener.should be_listening
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.