From 3e12494e442acc4532edb1aa245d291e7dae3a6b Mon Sep 17 00:00:00 2001 From: Gibheer Date: Mon, 30 May 2011 21:56:02 +0200 Subject: [PATCH] * changed the method IndexSearcher#initialize to only use hashes * added IndexSearcher#search for strings only --- lib/polecat/index_searcher.rb | 25 +++++++++++++++++++------ spec/index_searcher/new_spec.rb | 22 ++++++++++++++++------ spec/index_searcher/search_spec.rb | 10 ++++++++++ 3 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 spec/index_searcher/search_spec.rb diff --git a/lib/polecat/index_searcher.rb b/lib/polecat/index_searcher.rb index 94017f1..5e41ea5 100644 --- a/lib/polecat/index_searcher.rb +++ b/lib/polecat/index_searcher.rb @@ -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 diff --git a/spec/index_searcher/new_spec.rb b/spec/index_searcher/new_spec.rb index 6e7ab54..4c9a481 100644 --- a/spec/index_searcher/new_spec.rb +++ b/spec/index_searcher/new_spec.rb @@ -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 diff --git a/spec/index_searcher/search_spec.rb b/spec/index_searcher/search_spec.rb new file mode 100644 index 0000000..bb6cdd4 --- /dev/null +++ b/spec/index_searcher/search_spec.rb @@ -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