Class: Polecat::IndexReader
- Inherits:
-
Object
- Object
- Polecat::IndexReader
- Defined in:
- lib/polecat/index_reader.rb
Overview
reads an index directory
This class reads the content of an index directory and builds the necessary structures for the index type.
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(path) ⇒ IndexReader
constructor
initialize a new reader.
-
#locked? ⇒ Boolean
checks whether the directory is locked or not.
-
#read ⇒ Polecat::Index
read the content of the directory.
Constructor Details
#initialize(path) ⇒ IndexReader
initialize a new reader
Create a new reader for the given path. If the directory is empty, you will get an empty index, else all documents stored in that directory.
14 15 16 17 |
# File 'lib/polecat/index_reader.rb', line 14 def initialize path @path = path raise ArgumentError, 'no valid directory' unless File.directory? @path end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/polecat/index_reader.rb', line 7 def path @path end |
Instance Method Details
#locked? ⇒ Boolean
checks whether the directory is locked or not
39 40 41 42 43 44 45 |
# File 'lib/polecat/index_reader.rb', line 39 def locked? if File.exists? @path + '/index.lock' true else false end end |
#read ⇒ Polecat::Index
read the content of the directory
Read all files of the directory and return an index object.
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/polecat/index_reader.rb', line 24 def read raise IOError, 'index is locked' if locked? files = Dir[@path + '/*'] if files.count > 0 documents = [] files.each do |file| documents += Marshal.load(File.read(file)) end documents else [] end end |