From b171eeaa3dc1d0b589773c36856d15e014692338 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Tue, 24 May 2011 11:12:38 +0200 Subject: [PATCH] the index is now using the documents for searching --- lib/polecat/index.rb | 14 ++++++--- spec/document/attribute_get.rb | 19 ++++++++++++ spec/document/attribute_set_spec.rb | 21 +++++++++++++ spec/document/attributes_spec.rb | 13 ++++++++ .../document/document_ressource/field_spec.rb | 21 +++++++++++++ spec/document/new_spec.rb | 31 +++++++++++++++++++ spec/index/count_spec.rb | 4 +-- spec/index/flush_spec.rb | 7 +++-- spec/index/index_dir_spec.rb | 2 +- spec/index/read_spec.rb | 2 +- spec/index/search_spec.rb | 18 +++++++---- spec/index/write_spec.rb | 22 +++++++++---- 12 files changed, 151 insertions(+), 23 deletions(-) create mode 100644 spec/document/attribute_get.rb create mode 100644 spec/document/attribute_set_spec.rb create mode 100644 spec/document/attributes_spec.rb create mode 100644 spec/document/document_ressource/field_spec.rb create mode 100644 spec/document/new_spec.rb diff --git a/lib/polecat/index.rb b/lib/polecat/index.rb index 1ecf3e2..5b0c212 100644 --- a/lib/polecat/index.rb +++ b/lib/polecat/index.rb @@ -26,8 +26,12 @@ class Polecat File.exists?(@path + '/index.txt') end - def write term - @buffer << term + def write doc + if (doc.kind_of? Document) + @buffer << doc + else + raise ArgumentError, "not a document" + end end # read all stored documents from the index files into the index @@ -47,8 +51,10 @@ class Polecat def search term matches = [] linenr = 0 - @documents.each do |line| - matches << linenr if line =~ /#{term}/ + @documents.each do |doc| + doc.attributes.each do |key, value| + matches << linenr if value[:value] =~ /#{term}/ + end linenr += 1 end matches diff --git a/spec/document/attribute_get.rb b/spec/document/attribute_get.rb new file mode 100644 index 0000000..c4eec0f --- /dev/null +++ b/spec/document/attribute_get.rb @@ -0,0 +1,19 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe "Document#attribute_get" do + before do + @doc = Document.new :id => 23 + end + + it "returns the value of the named attribute" do + @doc.attribute_get(:id).should == 23 + end + + it "returns the value if a string is given" do + @doc.attribute_get('id').should == 23 + end + + it "raises an error if the attribute does not exist" do + lambda { @doc.attribute_get('foo') }.should raise_error(ArgumentError) + end +end diff --git a/spec/document/attribute_set_spec.rb b/spec/document/attribute_set_spec.rb new file mode 100644 index 0000000..1f33a52 --- /dev/null +++ b/spec/document/attribute_set_spec.rb @@ -0,0 +1,21 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe "Document#attribute_set" do + before do + @doc = Spec::FooDocument.new + end + + it "takes a name and a value as an argument" do + @doc.attribute_set :id, 23 + @doc.id.should == 23 + end + + it "takes a string as name" do + @doc.attribute_set 'id', 23 + @doc.id.should == 23 + end + + it "raises an error if the name does not exist" do + lambda { @doc.attribute_set :foo, 23 }.should raise_error(ArgumentError) + end +end diff --git a/spec/document/attributes_spec.rb b/spec/document/attributes_spec.rb new file mode 100644 index 0000000..eb0f44c --- /dev/null +++ b/spec/document/attributes_spec.rb @@ -0,0 +1,13 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe "Document#attributes" do + it "returns a hash with all attributes" do + class FooDoc + include Document + + field :bar + end + d = FooDoc.new + d.attributes.should == {:bar => Document::OPTIONS} + end +end diff --git a/spec/document/document_ressource/field_spec.rb b/spec/document/document_ressource/field_spec.rb new file mode 100644 index 0000000..197bbf9 --- /dev/null +++ b/spec/document/document_ressource/field_spec.rb @@ -0,0 +1,21 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +describe "DocumentRessource#field" do + it "creates a method to get an attribute value" do + class FooDoc + include Document + field :doc + end + FooDoc.new.respond_to?(:doc).should == true + end + + it "creates a method to set an attribute" do + class FooDoc + include Document + field :doc + end + d = FooDoc.new + d.doc = 'foo' + d.doc.should == 'foo' + end +end diff --git a/spec/document/new_spec.rb b/spec/document/new_spec.rb new file mode 100644 index 0000000..de505b9 --- /dev/null +++ b/spec/document/new_spec.rb @@ -0,0 +1,31 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe "Document#initialize" do + it "creates a new Document object" do + d = Spec::FooDocument.new + d.is_a?(Document).should == true + end + + it "takes a hash as argument" do + d = Spec::FooDocument.new( + :id => 1, + :name => 'foo', + :lastname => 'bar', + :description => 'this is a test' + ) + d.id.should == 1 + d.name.should == 'foo' + d.lastname.should == 'bar' + d.description.should == 'this is a test' + end + + it "sets the attributes to nil as default" do + d = Spec::FooDocument.new + d.id.should == nil + end + + it "raises an error, when the key is not found" do + lambda { Spec::FooDocument.new(:foo => :bar) }.should( + raise_error(ArgumentError)) + end +end diff --git a/spec/index/count_spec.rb b/spec/index/count_spec.rb index d6f17b3..e1c7829 100644 --- a/spec/index/count_spec.rb +++ b/spec/index/count_spec.rb @@ -12,14 +12,14 @@ describe "Index#count" do it "returns 1 when 1 entry is in the index" do i = Polecat::Index.new @path - i.write "foo" + i.write Spec::FooDocument.new(:id => 1) i.flush i.count.should == 1 end it "does not count entries, which got not flushed" do i = Polecat::Index.new @path - i.write "foo" + i.write Spec::FooDocument.new(:id => 1) i.count.should == 0 end end diff --git a/spec/index/flush_spec.rb b/spec/index/flush_spec.rb index 5e3fb81..598450f 100644 --- a/spec/index/flush_spec.rb +++ b/spec/index/flush_spec.rb @@ -4,19 +4,20 @@ describe "Document#flush" do before do @path = prepare_index_dir @file = @path + '/index.txt' + @doc = Spec::FooDocument.new(:id => 1) end it "does not write anything to the file, until the #flush was called" do i = Polecat::Index.new @path i.flush - i.write "foo" + i.write @doc File.read(@file).should == Marshal.dump([]) end it "writes the content in the buffer to the file" do i = Polecat::Index.new @path - i.write "foo" + i.write @doc i.flush - File.read(@file).should == Marshal.dump(["foo"]) + File.read(@file).should == Marshal.dump([@doc]) end end diff --git a/spec/index/index_dir_spec.rb b/spec/index/index_dir_spec.rb index dbe6000..feaa767 100644 --- a/spec/index/index_dir_spec.rb +++ b/spec/index/index_dir_spec.rb @@ -12,7 +12,7 @@ describe "Document#index_dir?" do it "returns true, if the directory contains an index" do i = Polecat::Index.new @path - i.write 'foo' + i.write Spec::FooDocument.new(:id => 1) i.flush i.index_dir?.should == true end diff --git a/spec/index/read_spec.rb b/spec/index/read_spec.rb index 7d563a9..6dd08d9 100644 --- a/spec/index/read_spec.rb +++ b/spec/index/read_spec.rb @@ -13,7 +13,7 @@ describe "Index#read" do it "loads all documents in the index directory" do i = Polecat::Index.new @path - i.write "foo" + i.write Spec::FooDocument.new(:id => 1) i.flush i = Polecat::Index.new @path i.read diff --git a/spec/index/search_spec.rb b/spec/index/search_spec.rb index ed6e82a..6e6f75e 100644 --- a/spec/index/search_spec.rb +++ b/spec/index/search_spec.rb @@ -1,6 +1,12 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe "Index#search" do + before :all do + @doc1 = Spec::FooDocument.new :id => 1, :description => 'foo' + @doc2 = Spec::FooDocument.new :id => 2, :description => 'bar' + @doc3 = Spec::FooDocument.new :id => 3, :description => 'foo bar baz' + end + before do @path = prepare_index_dir @file = @path + '/index.txt' @@ -8,24 +14,24 @@ describe "Index#search" do it "returns an array of lines, where the match was found" do i = Polecat::Index.new @path - i.write "foo" - i.write "bar" - i.write "foo" + i.write @doc1 + i.write @doc2 + i.write @doc1.clone i.flush i.search("foo").should == [0, 2] end it "returns an array of lines, where the match is somewhere in it" do i = Polecat::Index.new @path - i.write "foo bar baz" + i.write @doc3 i.flush i.search("baz").should == [0] end it "returns an empty array, when no match was found" do i = Polecat::Index.new @path - i.write "foo" - i.write "bar" + i.write @doc1 + i.write @doc2 i.flush i.search("baz").should == [] end diff --git a/spec/index/write_spec.rb b/spec/index/write_spec.rb index 26f0745..e0649c5 100644 --- a/spec/index/write_spec.rb +++ b/spec/index/write_spec.rb @@ -1,23 +1,33 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe "Document#write" do + before :all do + @doc1 = Spec::FooDocument.new(:id => 1) + @doc2 = Spec::FooDocument.new(:id => 2) + end + before do @path = prepare_index_dir @file = @path + '/index.txt' end - it "writes a string to a file" do + it "writes a document to a file" do i = Polecat::Index.new @path - i.write "foobar" + i.write @doc1 i.flush - File.read(@file).should == Marshal.dump(["foobar"]) + File.read(@file).should == Marshal.dump([@doc1]) end it "appends new entries to the end of the file" do i = Polecat::Index.new @path - i.write "foo" - i.write "bar" + i.write @doc1 + i.write @doc2 i.flush - File.read(@file).should == Marshal.dump(["foo", "bar"]) + File.read(@file).should == Marshal.dump([@doc1, @doc2]) + end + + it "raises an error, if the object is not a Polecat::Document" do + lambda { Polecat::Index.new(@path).write "foo" }.should( + raise_error(ArgumentError)) end end