about summary refs log tree commit
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-11-23 13:11:52 -0800
committerEric Wong <normalperson@yhbt.net>2010-11-23 13:11:52 -0800
commit861f7759a53b37f76a9b64710b2312a1fd1365a8 (patch)
tree9d28b608d52e3a263bf7eb2a1dfeab474053f9dc
parent9b6c5f2fdf5cac77fda7fb506fe09d22cd18b8ab (diff)
downloadmetropolis-861f7759a53b37f76a9b64710b2312a1fd1365a8.tar.gz
use common setup elements
Some other databases will be able to utilize these
variables.
-rw-r--r--lib/metropolis.rb2
-rw-r--r--lib/metropolis/common.rb8
-rw-r--r--lib/metropolis/tc/hdb.rb10
3 files changed, 12 insertions, 8 deletions
diff --git a/lib/metropolis.rb b/lib/metropolis.rb
index 96a2a6d..1bae3d3 100644
--- a/lib/metropolis.rb
+++ b/lib/metropolis.rb
@@ -8,7 +8,7 @@ module Metropolis
   def self.new(opts = {})
     opts = opts.dup
     rv = Object.new
-    uri = URI.parse(opts[:uri])
+    uri = opts[:uri] = URI.parse(opts[:uri])
     case uri.scheme
     when 'tc'
       opts[:path_pattern] = uri.path
diff --git a/lib/metropolis/common.rb b/lib/metropolis/common.rb
index cdae590..c4efcf3 100644
--- a/lib/metropolis/common.rb
+++ b/lib/metropolis/common.rb
@@ -2,6 +2,14 @@
 module Metropolis::Common
   include Rack::Utils # unescape
 
+  def setup(opts)
+    @uri = opts[:uri]
+    @headers = { 'Content-Type' => 'application/octet-stream' }
+    @headers.merge!(opts[:response_headers] || {})
+    @nr_slots = opts[:nr_slots] || 3
+    @readonly = !!opts[:readonly]
+  end
+
   def r(code, body = nil)
     body ||= "#{HTTP_STATUS_CODES[code]}\n"
     [ code,
diff --git a/lib/metropolis/tc/hdb.rb b/lib/metropolis/tc/hdb.rb
index 871876d..ebd7a17 100644
--- a/lib/metropolis/tc/hdb.rb
+++ b/lib/metropolis/tc/hdb.rb
@@ -9,9 +9,7 @@ module Metropolis::TC::HDB
   include Metropolis::Common
 
   def setup(opts)
-    @headers = { 'Content-Type' => 'application/octet-stream' }
-    @headers.merge!(opts[:response_headers] || {})
-    @nr_slots = opts[:nr_slots] || 3
+    super
     path_pattern = opts[:path_pattern]
     path_pattern.scan(/%\d*x/).size == 1 or
       raise ArgumentError, "only one '/%\d*x/' may appear in #{path_pattern}"
@@ -41,7 +39,7 @@ module Metropolis::TC::HDB
     @dbv = (0...@nr_slots).to_a.map do |slot|
       path = sprintf(path_pattern, slot)
       hdb = TCHDB.new
-      unless opts[:readonly]
+      unless @readonly
         hdb.open(path, TCHDB::OWRITER | TCHDB::OCREAT) or ex!(:open, hdb)
         if @optimize
           hdb.optimize(*@optimize) or ex!(:optimize, hdb)
@@ -52,9 +50,7 @@ module Metropolis::TC::HDB
     end
     @rd_flags = TCHDB::OREADER
     @wr_flags = TCHDB::OWRITER
-    if opts[:readonly]
-      extend(RO)
-    end
+    extend(RO) if @readonly
   end