March 2008
Intermediate to advanced
112 pages
3h 7m
English
In Chapter 1, you saw how to index all the text files under a directory. Unfortunately, most of the files in a filesystem aren’t text files, so let’s extend the indexer to handle some different file types. We also want to filter and sort files by different fields, such as their modification date. In this chapter, we’ll implement these extensions and more. We’ll call our application ferretfind.
7moduleFerretFind8classReader9@@subclasses=[]10@@readers={}1112defReader.inherited(subclass)13@@subclasses<<subclass14end1516defReader.load_readers(field_infos)17@@subclasses.eachdo|subclass|18reader=subclass.new(field_infos)19subclass::EXTENSIONS.eachdo|ext|20@@readers[ext.downcase]=reader21end22end23end2425defReader.get_reader(path)26@@readers[(File.extname(path)[/[^.]+/]||"").downcase]27end2829defReader.read(path)30document={31:path=>path,32:accessed=>File.atime(path).strftime("%Y%m%d"),33:modified=>File.mtime(path).strftime("%Y%m%d")34}35ifFile.readable?(path)andreader=Reader.get_reader(path)36document.merge!(reader.read(path))37end38returndocument39end4041protected42definitialize(field_infos);end43defread(path);{}end44defadd_field(field_infos,field,options)45field_infos.add_field(field,options)unlessfield_infos[field]46end47end48end
The FerretFind::Reader class is ...
Read now
Unlock full access