0
0
Fork 0

* changed the method IndexSearcher#initialize to only use hashes

* added IndexSearcher#search for strings only
This commit is contained in:
Gibheer 2011-05-30 21:56:02 +02:00
parent 6c685b4840
commit 3e12494e44
3 changed files with 45 additions and 12 deletions

View File

@ -5,6 +5,7 @@ class Polecat
# all documents stored in an index.
class IndexSearcher
attr_reader :reader
attr_reader :default_field
# creates a new Polecat::IndexSearcher
#
@ -15,12 +16,16 @@ class Polecat
# # the following has the same meaning
# IndexSearcher.new 'index_dir'
# IndexSearcher.new(IndexReader.new 'index_dir')
def initialize *args
first = args[0]
if first.class == Polecat::IndexReader
@reader = first
elsif first.class == String
@reader = Polecat::IndexReader.new first
def initialize options
if options.has_key? :path
@reader = Polecat::IndexReader.new(options[:path])
elsif options.has_key? :reader
@reader = options[:reader]
raise ArgumentError, 'no reader' unless @reader.kind_of?(Polecat::IndexReader)
end
if options.has_key? :default_field
@default_field = options[:default_field]
end
end
@ -29,5 +34,13 @@ class Polecat
def path
@reader.path
end
def search query
result = []
@reader.read.each do |doc|
result << doc if doc.attributes[@default_field].equals?(query)
end
result
end
end
end

View File

@ -5,13 +5,23 @@ describe "IndexSearcher#new" do
@path = prepare_index_dir
end
it "takes a path as an arugment" do
s = Polecat::IndexSearcher.new @path
s.path.should == @path
it "takes a hash with options as an argument" do
s = Polecat::IndexSearcher.new(
:path => @path,
:default_field => :description
)
s.path.should == @path
s.default_field.should == :description
end
it "takes an IndexReader as an argument" do
s = Polecat::IndexSearcher.new(Polecat::IndexReader.new @path)
s.path.should == @path
it "takes a reader in the options" do
r = Polecat::IndexReader.new(@path)
s = Polecat::IndexSearcher.new :reader => r
s.reader.should == r
end
it "raises an error, when the reader is not an IndexReader" do
lambda { Polecat::IndexSearcher.new(:reader => "foo") }.should(
raise_error(ArgumentError))
end
end

View File

@ -0,0 +1,10 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe "IndexSearcher#search" do
let(:path) { prepare_index_dir }
let(:s) { Polecat::IndexSearcher.new :path => path }
it "takes a string as an argument" do
s.search("foo").should == []
end
end