0
0
Fork 0

the index is now using the documents for searching

This commit is contained in:
Gibheer 2011-05-24 11:12:38 +02:00
parent 720228945f
commit b171eeaa3d
12 changed files with 151 additions and 23 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

31
spec/document/new_spec.rb Normal file
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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