lib/thinking_sphinx/configuration.rb in dpickett-thinking-sphinx-1.1.12 vs lib/thinking_sphinx/configuration.rb in dpickett-thinking-sphinx-1.1.23

- old
+ new

@@ -17,24 +17,27 @@ # allow star:: false # min prefix length:: 1 # min infix length:: 1 # mem limit:: 64M # max matches:: 1000 - # morphology:: stem_en + # morphology:: nil # charset type:: utf-8 # charset table:: nil # ignore chars:: nil # html strip:: false # html remove elements:: '' + # searchd_binary_name:: searchd + # indexer_binary_name:: indexer # # If you want to change these settings, create a YAML file at # config/sphinx.yml with settings for each environment, in a similar # fashion to database.yml - using the following keys: config_file, # searchd_log_file, query_log_file, pid_file, searchd_file_path, port, # allow_star, enable_star, min_prefix_len, min_infix_len, mem_limit, # max_matches, morphology, charset_type, charset_table, ignore_chars, - # html_strip, html_remove_elements, delayed_job_priority. + # html_strip, html_remove_elements, delayed_job_priority, + # searchd_binary_name, indexer_binary_name. # # I think you've got the idea. # # Each setting in the YAML file is optional - so only put in the ones you # want to change. @@ -52,15 +55,17 @@ IndexOptions = %w( charset_table charset_type docinfo enable_star exceptions html_index_attrs html_remove_elements html_strip ignore_chars min_infix_len min_prefix_len min_word_len mlock morphology ngram_chars ngram_len phrase_boundary phrase_boundary_step preopen stopwords wordforms ) + + CustomOptions = %w( disable_range ) attr_accessor :config_file, :searchd_log_file, :query_log_file, :pid_file, :searchd_file_path, :address, :port, :allow_star, :database_yml_file, :app_root, :bin_path, :model_directories, - :delayed_job_priority + :delayed_job_priority, :searchd_binary_name, :indexer_binary_name attr_accessor :source_options, :index_options attr_reader :environment, :configuration @@ -69,14 +74,23 @@ # def initialize(app_root = Dir.pwd) self.reset end - def reset - self.app_root = RAILS_ROOT if defined?(RAILS_ROOT) - self.app_root = Merb.root if defined?(Merb) - self.app_root ||= app_root + def self.configure(&block) + yield instance + instance.reset(instance.app_root) + end + + def reset(custom_app_root=nil) + if custom_app_root + self.app_root = custom_app_root + else + self.app_root = RAILS_ROOT if defined?(RAILS_ROOT) + self.app_root = Merb.root if defined?(Merb) + self.app_root ||= app_root + end @configuration = Riddle::Configuration.new @configuration.searchd.address = "127.0.0.1" @configuration.searchd.port = 3312 @configuration.searchd.pid_file = "#{self.app_root}/log/searchd.#{environment}.pid" @@ -92,13 +106,15 @@ Dir.glob("#{app_root}/vendor/plugins/*/app/models/") self.delayed_job_priority = 0 self.source_options = {} self.index_options = { - :charset_type => "utf-8", - :morphology => "stem_en" + :charset_type => "utf-8" } + + self.searchd_binary_name = "searchd" + self.indexer_binary_name = "indexer" parse_config self end @@ -139,10 +155,12 @@ # Make sure all models are loaded - without reloading any that # ActiveRecord::Base is already aware of (otherwise we start to hit some # messy dependencies issues). # def load_models + return if defined?(Rails) && Rails.configuration.cache_classes + self.model_directories.each do |base| Dir["#{base}**/*.rb"].each do |file| model_name = file.gsub(/^#{base}([\w_\/\\]+)\.rb/, '\1') next if model_name.nil? @@ -154,10 +172,12 @@ model_name.camelize.constantize rescue LoadError model_name.gsub!(/.*[\/\\]/, '').nil? ? next : retry rescue NameError next + rescue StandardError + puts "Warning: Error loading #{file}" end end end end @@ -211,14 +231,15 @@ return unless File.exists?(path) conf = YAML::load(ERB.new(IO.read(path)).result)[environment] conf.each do |key,value| - self.send("#{key}=", value) if self.methods.include?("#{key}=") + self.send("#{key}=", value) if self.respond_to?("#{key}=") set_sphinx_setting self.source_options, key, value, SourceOptions set_sphinx_setting self.index_options, key, value, IndexOptions + set_sphinx_setting self.index_options, key, value, CustomOptions set_sphinx_setting @configuration.searchd, key, value set_sphinx_setting @configuration.indexer, key, value end unless conf.nil? self.bin_path += '/' unless self.bin_path.blank? @@ -231,11 +252,11 @@ def set_sphinx_setting(object, key, value, allowed = {}) if object.is_a?(Hash) object[key.to_sym] = value if allowed.include?(key.to_s) else - object.send("#{key}=", value) if object.methods.include?("#{key}") - send("#{key}=", value) if self.methods.include?("#{key}") + object.send("#{key}=", value) if object.respond_to?("#{key}") + send("#{key}=", value) if self.respond_to?("#{key}") end end end end