From 800f77b65facc8da2856c85cfc6021a866e039dc Mon Sep 17 00:00:00 2001 From: Jordan Elver Date: Thu, 19 Jan 2012 22:12:06 +0000 Subject: [PATCH 1/3] Add option to specify an external rules file --- lib/rack/rewrite.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/rack/rewrite.rb b/lib/rack/rewrite.rb index 81fba5d..b702350 100644 --- a/lib/rack/rewrite.rb +++ b/lib/rack/rewrite.rb @@ -5,10 +5,15 @@ module Rack # A rack middleware for defining and applying rewrite rules. In many cases you # can get away with rack-rewrite instead of writing Apache mod_rewrite rules. class Rewrite - def initialize(app, &rule_block) + def initialize(app, options = {}, &rule_block) @app = app @rule_set = RuleSet.new @rule_set.instance_eval(&rule_block) if block_given? + + rules_file = ::File.expand_path(options[:rules]) + if ::File.exists?(rules_file) + @rule_set.instance_eval(::File.read(rules_file)) + end end def call(env) From fa414be8514bd1e4052c94be38a4a25535b6f164 Mon Sep 17 00:00:00 2001 From: Jordan Elver Date: Thu, 19 Jan 2012 22:15:18 +0000 Subject: [PATCH 2/3] Update TODO --- TODO | 1 - 1 file changed, 1 deletion(-) diff --git a/TODO b/TODO index 88b5c14..d8e05d4 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,5 @@ * OUTSTANDING * Add :host support to restrict which URL's a rewrite rule matches [10/15/09] - * Add support for specifying a config file instead of passing a block (e.g. config/rewrite.rb) [10/15/09] * Provide testing helpers (e.g. should_rewrite) to facilitate straightforward testing. [10/25/09] * Allow rules to return arbitrary html (e.g. the contents of the maintenance page) [10/25/09] From 06904421ba2317bd3e6d1ae285853d47bc8995b3 Mon Sep 17 00:00:00 2001 From: Jordan Elver Date: Thu, 19 Jan 2012 22:21:21 +0000 Subject: [PATCH 3/3] Update README with how to specify an external config file --- README.rdoc | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.rdoc b/README.rdoc index fd054df..d044867 100644 --- a/README.rdoc +++ b/README.rdoc @@ -31,6 +31,15 @@ can get away with rack-rewrite instead of writing Apache mod_rewrite rules. r301 %r{/wiki/(\w+)_\w+}, '/$1' end +=== Specify an external rules file AND additional rules in the block + + config.middleware.insert_before(Rack::Lock, Rack::Rewrite, :rules => 'config/rules.rb') do + rewrite '/wiki/John_Trupiano', '/john' + r301 '/wiki/Yair_Flicker', '/yair' + r302 '/wiki/Greg_Jastrab', '/greg' + r301 %r{/wiki/(\w+)_\w+}, '/$1' + end + == Use Cases === Rebuild of existing site in a new technology