0
0
Fork 0

Document was replaced with Virtus, an attribute Gem from solnic

see http://github.com/solnic/virtus for more informations
This commit is contained in:
Gibheer 2011-06-06 18:47:01 +02:00
parent ffa9e96a9f
commit 68306d1f08
15 changed files with 43 additions and 142 deletions

View File

@ -1,5 +1,10 @@
source "http://rubygems.org"
# comment out the gems, you don't want
group :preferred do
gem "virtus"
end
group :development do
gem "rspec"
gem "rdoc"

View File

@ -2,7 +2,6 @@ class Polecat
require 'polecat/index_writer'
require 'polecat/index_reader'
require 'polecat/index_searcher'
require 'polecat/document'
require 'polecat/query'
require 'polecat/term'
end

View File

@ -46,7 +46,7 @@ class Polecat
#doc.attributes.fetch(@default_field).fetch(:value) == query
rs = []
query.terms.each do |term|
val = doc.attributes.fetch(term.field.to_sym).fetch(:value)
val = doc.send(term.field.to_sym)
if compare val, term.operator, term.value
rs << true
end

View File

@ -40,10 +40,10 @@ class Polecat
# to the filesystem.
# @param [Document] doc the document to store
def add doc
if doc.kind_of? Polecat::Document
if doc.respond_to? :attributes
@documents << doc
else
raise ArgumentError, 'not a document'
raise ArgumentError, 'missing method attributes'
end
end

View File

@ -1,19 +0,0 @@
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

@ -1,21 +0,0 @@
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

@ -1,13 +0,0 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe "Document#attributes" do
it "returns a hash with all attributes" do
class FooDoc
include Polecat::Document
field :bar
end
d = FooDoc.new
d.attributes.should == {:bar => Polecat::Document::OPTIONS}
end
end

View File

@ -1,21 +0,0 @@
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 Polecat::Document
field :doc
end
FooDoc.new.respond_to?(:doc).should == true
end
it "creates a method to set an attribute" do
class FooDoc
include Polecat::Document
field :doc
end
d = FooDoc.new
d.doc = 'foo'
d.doc.should == 'foo'
end
end

View File

@ -1,31 +0,0 @@
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?(Polecat::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

@ -17,7 +17,7 @@ describe "IndexReader#read" do
it "returns the document count found in the index directory" do
w = Polecat::IndexWriter.new @path
w.add Spec::FooDocument.new(:id => 23)
w.add Spec::TestDocument.new(:id => 23)
w.write
r = Polecat::IndexReader.new @path
r.read.count.should == 1
@ -25,16 +25,16 @@ describe "IndexReader#read" do
it "returns an array of documents" do
w = Polecat::IndexWriter.new @path
w.add Spec::FooDocument.new(:id => 23)
w.add Spec::TestDocument.new(:id => 23)
w.write
w.create_reader.read[0].kind_of?(Polecat::Document).should == true
w.create_reader.read[0].respond_to?(:attributes).should == true
end
it "merges all documents from different files together" do
w = Polecat::IndexWriter.new @path
w.add Spec::FooDocument.new(:id => 23)
w.add Spec::TestDocument.new(:id => 23)
w.write
w.add Spec::FooDocument.new(:id => 24)
w.add Spec::TestDocument.new(:id => 24)
w.write
w.create_reader.read.count.should == 2
end

View File

@ -11,10 +11,10 @@ describe "IndexSearcher#search" do
context "searching on a filled index" do
before do
w.add Spec::FooDocument.new(:id => 0, :name => 'foo')
w.add Spec::FooDocument.new(:id => 1, :name => 'bar')
w.add Spec::FooDocument.new(:id => 2, :name => 'baz')
w.add Spec::FooDocument.new(:id => 3, :name => 'foobar')
w.add Spec::TestDocument.new(:id => 0, :name => 'foo')
w.add Spec::TestDocument.new(:id => 1, :name => 'bar')
w.add Spec::TestDocument.new(:id => 2, :name => 'baz')
w.add Spec::TestDocument.new(:id => 3, :name => 'foobar')
w.write
end

View File

@ -1,23 +1,24 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe "IndexWriter#add" do
before :all do
@doc1 = Spec::FooDocument.new :id => 1
@doc2 = Spec::FooDocument.new :id => 2
end
before do
@path = prepare_index_dir
end
let (:path) { prepare_index_dir }
let (:doc1) { Spec::TestDocument.new :id => 1 }
let (:doc2) { Spec::TestDocument.new :id => 2 }
let (:w) { w = Polecat::IndexWriter.new path }
it "adds the object to the list of objects" do
w = Polecat::IndexWriter.new @path
w.add @doc1
w.add doc1
w.count.should == 1
end
it "takes multiple documents and sotres them" do
w.add doc1
w.add doc2
w.count.should == 2
end
it "raises an error, when the object is not a document" do
lambda { Polecat::IndexWriter.new(@path).add "foo" }.should(
lambda { Polecat::IndexWriter.new(path).add "foo" }.should(
raise_error(ArgumentError))
end
end

View File

@ -12,7 +12,7 @@ describe "IndexWriter#count" do
it "returns the number of documents stored in the storage" do
w = Polecat::IndexWriter.new @path
w.add Spec::FooDocument.new
w.add Spec::TestDocument.new
w.count.should == 1
end
end

View File

@ -7,20 +7,20 @@ describe "IndexWriter#write" do
it "sets count to 0" do
w = Polecat::IndexWriter.new @path
w.add Spec::FooDocument.new
w.add Spec::TestDocument.new
w.write
w.count.should == 0
end
it "returns true when the write was a success" do
w = Polecat::IndexWriter.new @path
w.add Spec::FooDocument.new
w.add Spec::TestDocument.new
w.write.should == true
end
it "removes the lock after a write" do
w = Polecat::IndexWriter.new @path
w.add Spec::FooDocument.new
w.add Spec::TestDocument.new
w.write
File.exists?(@path + '/index.lock').should == false
end
@ -28,14 +28,14 @@ describe "IndexWriter#write" do
it "takes a higher number for the index file, if there is already one" do
FileUtils.touch @path + '/ind0.ind'
w = Polecat::IndexWriter.new @path
w.add Spec::FooDocument.new
w.add Spec::TestDocument.new
w.write
File.exists?(@path + '/ind1.ind').should == true
end
it "writes a marshalled representation of the document list" do
w = Polecat::IndexWriter.new @path
doc = Spec::FooDocument.new
doc = Spec::TestDocument.new
w.add doc
w.write
File.read(@path + '/ind0.ind').should == Marshal.dump([doc])
@ -44,7 +44,7 @@ describe "IndexWriter#write" do
it "returns false when the directory has an 'index.lock' file" do
w = Polecat::IndexWriter.new @path
FileUtils.touch @path + '/index.lock'
w.add Spec::FooDocument.new
w.add Spec::TestDocument.new
w.write.should == false
end
end

View File

@ -3,6 +3,7 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
require 'rspec'
require 'fileutils'
require 'polecat'
require 'virtus'
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
@ -26,12 +27,12 @@ def prepare_index_dir
end
module Spec
class FooDocument
include Polecat::Document
class TestDocument
include Virtus
field :id
field :name, :lazy => true
field :lastname, :lazy => true
field :description, :analyze => true, :lazy => true
attribute :id, Integer
attribute :name, String
attribute :lastname, String
attribute :description, String, :analyze => true, :lazy => true
end
end