0
0
Fork 0

some smaller clean up in the classes and documentation

This commit is contained in:
Gibheer 2011-05-24 11:54:42 +02:00
parent b171eeaa3d
commit b11090d145
2 changed files with 30 additions and 9 deletions

View File

@ -7,24 +7,40 @@ module Document
:value => nil
}
def self.included o
o.extend(DocumentResource)
o.instance_variable_set :@attributes, {}
def self.included klass # :nodoc:
klass.extend(DocumentResource)
klass.instance_variable_set :@attributes, {}
end
# creates a new document
#
# It is possible to create a new document with a hash, which has all values
# of the fields.
# Example:
# class Foo
# include Polecat::Document
#
# field :id
# field :description
# end
# f = Foo.new :id => 1, :description => 'foo'
def initialize fields = {}
fields.each do |key, value|
attribute_set key, value
end
end
# get an attribute of the document
def attribute_get name
attributes[name.to_sym][:value]
end
# set an attribute of the document
def attribute_set name, value
if attributes.has_key? name.to_sym
attributes[name.to_sym][:value] = value
name = name.to_sym
att = attributes
if att.has_key? name
att[name][:value] = value
else
raise ArgumentError, "attribute #{name} does not exist"
end

View File

@ -1,4 +1,8 @@
class Polecat
# storage of documents
#
# This is the core of the search platform, the index. It stores the documents,
# stores and reads them and make them searchable.
class Index
attr_reader :path
@ -36,15 +40,16 @@ class Polecat
# read all stored documents from the index files into the index
def read
if (File.exists?(@path + '/index.txt'))
@documents = Marshal.load(File.read(@path+'/index.txt'))
index_file = @path + '/index.txt'
if (File.exists? index_file)
@documents = Marshal.load(File.read(index_file))
end
end
def flush
@documents += @buffer
File.open @path + '/index.txt', 'w' do |f|
f.write Marshal.dump(@documents)
File.open @path + '/index.txt', 'w' do |file|
file.write Marshal.dump(@documents)
end
end