From 88fa90b7f039f62962cc8d11031446412b951be2 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 6 Dec 2010 15:43:46 -0800 Subject: allow easier, single-file options for TC and TDB Most (other) users only need a single file, even though my primary use of this is for multiple files. --- lib/metropolis.rb | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) (limited to 'lib/metropolis.rb') diff --git a/lib/metropolis.rb b/lib/metropolis.rb index 425ce1c..c38d4af 100644 --- a/lib/metropolis.rb +++ b/lib/metropolis.rb @@ -13,28 +13,22 @@ module Metropolis def self.new(opts = {}) opts = opts.dup rv = Object.new - uri = opts[:uri] = URI.parse(opts[:uri]) - if uri.path != '/' && opts[:path_pattern] - raise ArgumentError, ":path_pattern may only be used if path is '/'" + uri = URI.parse(opts[:uri]) + rv.instance_eval do + @uri = uri + @query = @uri.query ? Rack::Utils.parse_query(@uri.query) : nil + @path_pattern = opts[:path_pattern] + @path = @uri.path if @uri.path != '/' end - case uri.scheme - when 'hash' - opts[:path] = uri.path if uri.path != '/' - rv.extend Metropolis::Hash - when 'tdb' - opts[:query] = Rack::Utils.parse_query(uri.query) if uri.query - rv.extend Metropolis::TDB - when 'tc' - opts[:query] = Rack::Utils.parse_query(uri.query) if uri.query - case ext = File.extname(opts[:path_pattern] || uri.path) - when '.tch' - rv.extend Metropolis::TC::HDB - else - raise ArgumentError, "unsupported suffix: #{ext}" - end + + base = case uri.scheme + when 'hash' then Metropolis::Hash + when 'tdb' then Metropolis::TDB + when 'tc' then Metropolis::TC else raise ArgumentError, "unsupported URI scheme: #{uri.scheme}" end + rv.extend(base) rv.setup(opts) rv end -- cgit v1.2.3-24-ge0c7