Class: Filerary::Librarian

Inherits:
Object
  • Object
show all
Defined in:
lib/filerary/librarian.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_dirObject (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_pathObject (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

#cleanupObject



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.expand_path(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