set options when adding the middleware to the stack

This commit is contained in:
John Bintz 2011-11-07 09:06:40 -05:00
parent 17d4bed4eb
commit ccf888dd30
3 changed files with 30 additions and 3 deletions

View File

@ -12,6 +12,15 @@ In `config/environments/development.rb`:
``` ruby
MyApp::Application.configure do
config.middleware.insert_before(Rack::Lock, Rack::LiveReload)
# ...or, change some options...
config.middleware.insert_before(
Rack::Lock, Rack::LiveReload,
:min_delay => 500,
:max_delay => 10000,
:port => 56789
)
end
```
@ -21,6 +30,8 @@ end
require 'rack-livereload'
use Rack::LiveReload
# ...or...
use Rack::LiveReload, :min_delay => 500, ...
```
## How it works
@ -33,9 +44,7 @@ you'll connect and be LiveReloading away!
## To-do
* Specify the `port`
* Override the `host`
* Change the reload delays
As usual, super-alpha!

View File

@ -4,8 +4,9 @@ module Rack
attr_reader :app
def initialize(app)
def initialize(app, options = {})
@app = app
@options = options
end
def call(env)
@ -22,6 +23,9 @@ module Rack
if !headers['X-Rack-LiveReload'] && line['</head>']
src = LIVERELOAD_JS_PATH.dup
src << "?host=#{env['HTTP_HOST'].gsub(%r{:.*}, '')}" if env['HTTP_HOST']
src << "&mindelay=#{@options[:min_delay]}" if @options[:min_delay]
src << "&maxdelay=#{@options[:max_delay]}" if @options[:max_delay]
src << "&port=#{@options[:port]}" if @options[:port]
line.gsub!('</head>', %{<script type="text/javascript" src="#{src}"></script></head>})

View File

@ -42,6 +42,19 @@ describe Rack::LiveReload do
described_class::LIVERELOAD_JS_PATH.should_not include(host)
end
context 'set options' do
let(:middleware) { described_class.new(app, :port => port, :min_delay => min_delay, :max_delay => max_delay) }
let(:min_delay) { 5 }
let(:max_delay) { 10 }
let(:port) { 23 }
it 'should add the livereload.js script tag' do
body.should include("mindelay=#{min_delay}")
body.should include("maxdelay=#{max_delay}")
body.should include("port=#{port}")
end
end
end
context '/__rack/livereload.js' do
@ -56,3 +69,4 @@ describe Rack::LiveReload do
end
end
end