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



86
87
88
89
90
91
# File 'lib/filerary/librarian.rb', line 86

def cleanup
  @files.grn.records.each do |record|
    path = record._key
    @files.delete(path) unless File.exist?(path)
  end
end

#collect(paths) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/filerary/librarian.rb', line 46

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)

    content = read_content(path)
    next unless content

    @files[path] = {
      :content    => content,
      :updated_at => Time.now,
    }
  end
end

#destroyObject



98
99
100
101
# File 'lib/filerary/librarian.rb', line 98

def destroy
  FileUtils.rm(Dir.glob("#{@db_path}*"))
  FileUtils.rmdir(@db_dir)
end

#listObject



40
41
42
43
44
# File 'lib/filerary/librarian.rb', line 40

def list
  @files.collect do |record|
    record._key
  end
end

#remove(path) ⇒ Object

Raises:

  • (ArgumentError)


93
94
95
96
# File 'lib/filerary/librarian.rb', line 93

def remove(path)
  raise ArgumentError, "file not found" unless @files[path]
  @files.delete(path)
end

#search(word) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/filerary/librarian.rb', line 66

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

#show(path) ⇒ Object

Raises:

  • (ArgumentError)


76
77
78
79
80
# File 'lib/filerary/librarian.rb', line 76

def show(path)
  file = @files[path]
  raise ArgumentError, "file not found" unless file
  file.content
end

#sizeObject



36
37
38
# File 'lib/filerary/librarian.rb', line 36

def size
  @files.size
end

#updateObject



82
83
84
# File 'lib/filerary/librarian.rb', line 82

def update
  collect(list)
end