From ccf888dd30a94574153af45436b01ca35af2245f Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 7 Nov 2011 09:06:40 -0500 Subject: [PATCH] set options when adding the middleware to the stack --- README.md | 13 +++++++++++-- lib/rack/livereload.rb | 6 +++++- spec/rack/livereload_spec.rb | 14 ++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d6dc5c3..0796f41 100644 --- a/README.md +++ b/README.md @@ -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! diff --git a/lib/rack/livereload.rb b/lib/rack/livereload.rb index a1592c6..96d1d7a 100644 --- a/lib/rack/livereload.rb +++ b/lib/rack/livereload.rb @@ -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[''] 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!('', %{}) diff --git a/spec/rack/livereload_spec.rb b/spec/rack/livereload_spec.rb index 8bafd88..a6d7207 100644 --- a/spec/rack/livereload_spec.rb +++ b/spec/rack/livereload_spec.rb @@ -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 +