commit 8925a432832445cbf75c298aef1607b0feebe5b5
parent 1a1bbb65a5be1b138d0c8ac3fbf107bfb84fb3ee
Author: William Morgan <wmorgan-sup@masanjin.net>
Date: Sun, 3 Jan 2010 08:25:17 -0500
have Index.init take an argument specifying the index type
Yet another layer of metaprogramming wankery to make this happen. If nil or
"auto", will autodetect based on the existence of ferret/ or xapian/
directories, in a nasty abstraction barrier violation.
Diffstat:
2 files changed, 26 insertions(+), 8 deletions(-)
diff --git a/lib/sup.rb b/lib/sup.rb
@@ -54,7 +54,7 @@ module Redwood
YAML_DOMAIN = "masanjin.net"
YAML_DATE = "2006-10-01"
- DEFAULT_INDEX = 'ferret'
+ DEFAULT_NEW_INDEX_TYPE = 'xapian'
## record exceptions thrown in threads nicely
@exceptions = []
diff --git a/lib/sup/index.rb b/lib/sup/index.rb
@@ -176,13 +176,31 @@ class BaseIndex
end
end
-index_name = ENV['SUP_INDEX'] || $config[:index] || DEFAULT_INDEX
-case index_name
- when "xapian"; require "sup/xapian_index"
- when "ferret"; require "sup/ferret_index"
- else fail "unknown index type #{index_name.inspect}"
+## just to make the backtraces even more insane, here we engage in yet more
+## method_missing metaprogramming so that Index.init(index_type_name) will
+## magically make Index act like the correct Index class.
+class Index
+ def self.init type=nil
+ ## determine the index type from the many possible ways of setting it
+ type = (type == "auto" ? nil : type) ||
+ ENV['SUP_INDEX'] ||
+ $config[:index] ||
+ (File.exist?(File.join(BASE_DIR, "xapian")) && "xapian") || ## PRIORITIZE THIS
+ (File.exist?(File.join(BASE_DIR, "ferret")) && "ferret") || ## deprioritize this
+ DEFAULT_NEW_INDEX_TYPE
+ begin
+ require "sup/#{type}_index"
+ @klass = Redwood.const_get "#{type.capitalize}Index"
+ @obj = @klass.init
+ rescue LoadError, NameError => e
+ raise "unknown index type #{type.inspect}: #{e.message}"
+ end
+ debug "using #{type} index"
+ @obj
+ end
+
+ def self.instance; @obj end
+ def self.method_missing m, *a, &b; @obj.send(m, *a, &b) end
end
-Index = Redwood.const_get "#{index_name.capitalize}Index"
-debug "using index #{Index.name}"
end