Class: Filerary::Librarian
- Inherits:
-
Object
- Object
- Filerary::Librarian
- Defined in:
- lib/filerary/librarian.rb
Instance Attribute Summary collapse
-
#db_dir ⇒ Object
readonly
Returns the value of attribute db_dir.
-
#db_path ⇒ Object
readonly
Returns the value of attribute db_path.
Instance Method Summary collapse
- #cleanup ⇒ Object
- #collect(paths) ⇒ Object
-
#initialize(base_dir = default_base_dir) ⇒ Librarian
constructor
A new instance of Librarian.
- #search(word) ⇒ Object
Constructor Details
#initialize(base_dir = default_base_dir) ⇒ Librarian
Returns a new instance of Librarian.
26 27 28 29 30 31 32 33 34 |
# File 'lib/filerary/librarian.rb', line 26 def initialize(base_dir=default_base_dir) @db_dir = File.join(base_dir, "db") FileUtils.mkdir_p(@db_dir) @db_path = File.join(@db_dir, "filerary.db") GrnMini.create_or_open(@db_path) @files = GrnMini::Hash.new("Files") end |
Instance Attribute Details
#db_dir ⇒ Object (readonly)
Returns the value of attribute db_dir.
25 26 27 |
# File 'lib/filerary/librarian.rb', line 25 def db_dir @db_dir end |
#db_path ⇒ Object (readonly)
Returns the value of attribute db_path.
25 26 27 |
# File 'lib/filerary/librarian.rb', line 25 def db_path @db_path end |
Instance Method Details
#cleanup ⇒ Object
63 64 65 66 67 68 |
# File 'lib/filerary/librarian.rb', line 63 def cleanup @files.grn.records.each do |record| path = record._key @files.delete(path) unless File.exist?(path) end end |
#collect(paths) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/filerary/librarian.rb', line 36 def collect(paths) paths = [paths] if paths.is_a?(String) paths.each do |path| path = File.(path) next unless File.file?(path) next if /\/\.git\// =~ path next if @files[path] && @files[path].updated_at > File.mtime(path) @files[path] = { :content => read_content(path), :updated_at => Time.now, } end end |
#search(word) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/filerary/librarian.rb', line 53 def search(word) result = @files.select do |record| record.content =~ word end result.collect do |record| record._key.force_encoding(Encoding.find("locale")) end end |