Skip to content

Commit

Permalink
Adding Xapit.setup and Xapit.remove_database so one doesn't need to i…
Browse files Browse the repository at this point in the history
…nteract directly with Xapit::Config
  • Loading branch information
ryanb committed Jun 12, 2009
1 parent 2bd7070 commit 954934f
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 27 deletions.
6 changes: 3 additions & 3 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ To perform the indexing, run the xapit:index rake task.

It can also be triggered through Ruby code using this command.

Xapit::Config.remove_database
Xapit.remove_database
Xapit.index_all


Expand Down Expand Up @@ -140,11 +140,11 @@ You can also list the applied facets along with a remove link.

When installing Xapit as a Rails plugin, an initializer file is automatically created to setup. It looks like this.

Xapit::Config.setup(:database_path => "#{Rails.root}/db/xapiandb")
Xapit.setup(:database_path => "#{Rails.root}/db/xapiandb")

There are many other options you can pass into here. This is a more advanced configuration setting which changes the stemming language, disables spelling, and changes the indexer and parser to a classic variation. The classic ones use Xapian's built-in term generator and query parser instead of the ones offered by Xapit.

Xapit::Config.setup(
Xapit.setup(
:database_path => "#{Rails.root}/db/external/xapiandb",
:spelling => false,
:stemming => "german",
Expand Down
6 changes: 3 additions & 3 deletions features/step_definitions/xapit_steps.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Given /^I configured the database to be saved at "([^\"]*)"$/ do |path|
Xapit::Config.setup(:database_path => File.dirname(__FILE__) + "/../../#{path}")
Xapit.setup(:database_path => File.dirname(__FILE__) + "/../../#{path}")
end

Given /^an empty database at "([^\"]*)"$/ do |path|
Xapit::Config.setup(:database_path => File.dirname(__FILE__) + "/../../#{path}")
Xapit::Config.remove_database
Xapit.setup(:database_path => File.dirname(__FILE__) + "/../../#{path}")
Xapit.remove_database
XapitMember.delete_all
end

Expand Down
2 changes: 1 addition & 1 deletion features/support/xapit_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module XapitHelpers
def create_records(records, perform_index = true)
Xapit::Config.remove_database
Xapit.remove_database
XapitMember.delete_all
XapitMember.xapit do |index|
records.first.keys.each do |attribute|
Expand Down
2 changes: 1 addition & 1 deletion install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
puts "Adding setup_xapit.rb initializer."
File.open(path, "w") do |f|
f.write <<-EOS
Xapit::Config.setup(:database_path => "\#{Rails.root}/db/xapiandb")
Xapit.setup(:database_path => "\#{Rails.root}/db/xapiandb")
EOS
end
end
21 changes: 19 additions & 2 deletions lib/xapit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

# Looking for more documentation? A good place to start is Xapit::Membership
module Xapit

# Index all membership classes with xapit defined. Delegates to Xapit::IndexBlueprint.
# You will likely want to call Xapit::Config.remove_database before this.
# You will likely want to call Xapit.remove_database before this.
def self.index_all(*args, &block)
IndexBlueprint.index_all(*args, &block)
end
Expand All @@ -21,6 +20,24 @@ def self.index_all(*args, &block)
def self.search(*args)
Collection.new(nil, *args)
end

# Setup configuration options. The following options are supported.
#
# <tt>:database_path</tt>: Where the database is stored.
# <tt>:stemming</tt>: The language to use for stemming, defaults to "english".
# <tt>:spelling</tt>: True or false to enable/disable spelling, defaults to true.
# <tt>:indexer</tt>: Class to handle the indexing, defaults to SimpleIndexer.
# <tt>:query_parser</tt>: Class to handle the parsing, defaults to ClassicQueryParser.
# <tt>:breadcrumb_facets</tt>: Use breadcrumb mode for applied facets. See Collection#applied_facet_options for details.
#
def self.setup(*args)
Config.setup(*args)
end

# Removes the configured database file and clears the stored one in memory.
def self.remove_database
Config.remove_database
end
end

require File.dirname(__FILE__) + '/xapit/membership'
Expand Down
2 changes: 1 addition & 1 deletion lib/xapit/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def facets
# If you set :breadcrumb_facets option to true in Config#setup the link will drop leftover facets
# instead of removing the current one. This makes it easy to add a breadcrumb style interface.
#
# Config.setup(:breadcrumb_facets => true)
# Xapit.setup(:breadcrumb_facets => true)
# <% for option in @articles.applied_facet_options %>
# <%= link_to h(option.name), :overwrite_params => { :facets => option } %> &gt;
# <% end %>
Expand Down
10 changes: 1 addition & 9 deletions lib/xapit/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@ class Config
class << self
attr_reader :options

# Setup configuration options. The following options are supported.
#
# <tt>:database_path</tt>: Where the database is stored.
# <tt>:stemming</tt>: The language to use for stemming, defaults to "english".
# <tt>:spelling</tt>: True or false to enable/disable spelling, defaults to true.
# <tt>:indexer</tt>: Class to handle the indexing, defaults to SimpleIndexer.
# <tt>:query_parser</tt>: Class to handle the parsing, defaults to ClassicQueryParser.
# <tt>:breadcrumb_facets</tt>: Use breadcrumb mode for applied facets. See Collection#applied_facet_options for details.
#
# See Xapit#setup
def setup(options = {})
if @options && options[:database_path] != @options[:database_path]
@database = nil
Expand Down
2 changes: 1 addition & 1 deletion lib/xapit/index_blueprint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def sortable(*attributes)
end

# Indexes all records of this blueprint class. It does this using the ".find_each" method on the member class.
# You will likely want to call Xapit::Config.remove_database before this.
# You will likely want to call Xapit.remove_database before this.
def index_all
@member_class.xapit_adapter.find_each(*@args) do |member|
@indexer.add_member(member)
Expand Down
2 changes: 1 addition & 1 deletion lib/xapit/query_parsers/abstract_query_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def facet_identifiers
end

def spelling_suggestion
raise "Spelling has been disabled. Enable spelling in Xapit::Config.setup." unless Config.spelling?
raise "Spelling has been disabled. Enable spelling in Xapit.setup." unless Config.spelling?
if [@search_text, *@search_text.scan(/\w+/)].all? { |term| term_suggestion(term).nil? }
nil
else
Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
Spec::Runner.configure do |config|
config.mock_with :rr
config.before(:each) do
Xapit::Config.setup(:database_path => File.dirname(__FILE__) + '/tmp/xapiandb')
Xapit::Config.remove_database
Xapit.setup(:database_path => File.dirname(__FILE__) + '/tmp/xapiandb')
Xapit.remove_database
XapitMember.delete_all
end
end
4 changes: 2 additions & 2 deletions spec/xapit/facet_option_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
end

it "should remove current identifier from previous identifiers if it exists" do
Xapit::Config.setup(:breadcrumb_facets => false)
Xapit.setup(:breadcrumb_facets => false)
option = Xapit::FacetOption.new(nil, nil, nil)
option.existing_facet_identifiers = ["abc", "123", "foo"]
stub(option).identifier { "foo" }
option.to_param.should == "abc-123"
end

it "should support breadcrumb style facets" do
Xapit::Config.setup(:breadcrumb_facets => true)
Xapit.setup(:breadcrumb_facets => true)
option = Xapit::FacetOption.new(nil, nil, nil)
option.existing_facet_identifiers = ["abc", "123", "foo"]
stub(option).identifier { "123" }
Expand Down
2 changes: 1 addition & 1 deletion tasks/xapit.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ unless @xapit_rake_loaded
namespace :xapit do
desc "Index all xapit models."
task :index => :environment do
Xapit::Config.remove_database
Xapit.remove_database
Xapit.index_all do |member_class|
puts "Indexing #{member_class.name}"
end
Expand Down

0 comments on commit 954934f

Please sign in to comment.