some smaller clean up in the classes and documentation
This commit is contained in:
parent
b171eeaa3d
commit
b11090d145
|
@ -7,24 +7,40 @@ module Document
|
||||||
:value => nil
|
:value => nil
|
||||||
}
|
}
|
||||||
|
|
||||||
def self.included o
|
def self.included klass # :nodoc:
|
||||||
o.extend(DocumentResource)
|
klass.extend(DocumentResource)
|
||||||
o.instance_variable_set :@attributes, {}
|
klass.instance_variable_set :@attributes, {}
|
||||||
end
|
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 = {}
|
def initialize fields = {}
|
||||||
fields.each do |key, value|
|
fields.each do |key, value|
|
||||||
attribute_set key, value
|
attribute_set key, value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# get an attribute of the document
|
||||||
def attribute_get name
|
def attribute_get name
|
||||||
attributes[name.to_sym][:value]
|
attributes[name.to_sym][:value]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# set an attribute of the document
|
||||||
def attribute_set name, value
|
def attribute_set name, value
|
||||||
if attributes.has_key? name.to_sym
|
name = name.to_sym
|
||||||
attributes[name.to_sym][:value] = value
|
att = attributes
|
||||||
|
if att.has_key? name
|
||||||
|
att[name][:value] = value
|
||||||
else
|
else
|
||||||
raise ArgumentError, "attribute #{name} does not exist"
|
raise ArgumentError, "attribute #{name} does not exist"
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,8 @@
|
||||||
class Polecat
|
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
|
class Index
|
||||||
attr_reader :path
|
attr_reader :path
|
||||||
|
|
||||||
|
@ -36,15 +40,16 @@ class Polecat
|
||||||
|
|
||||||
# read all stored documents from the index files into the index
|
# read all stored documents from the index files into the index
|
||||||
def read
|
def read
|
||||||
if (File.exists?(@path + '/index.txt'))
|
index_file = @path + '/index.txt'
|
||||||
@documents = Marshal.load(File.read(@path+'/index.txt'))
|
if (File.exists? index_file)
|
||||||
|
@documents = Marshal.load(File.read(index_file))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def flush
|
def flush
|
||||||
@documents += @buffer
|
@documents += @buffer
|
||||||
File.open @path + '/index.txt', 'w' do |f|
|
File.open @path + '/index.txt', 'w' do |file|
|
||||||
f.write Marshal.dump(@documents)
|
file.write Marshal.dump(@documents)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue