class Filerary::Librarian

Attributes

db_dir[R]
db_path[R]

Public Class Methods

new(base_dir=default_base_dir) click to toggle source
# 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

Public Instance Methods

cleanup() click to toggle source
# 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) click to toggle source
# 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
destroy() click to toggle source
# File lib/filerary/librarian.rb, line 98
def destroy
  FileUtils.rm(Dir.glob("#{@db_path}*"))
  FileUtils.rmdir(@db_dir)
end
list() click to toggle source
# File lib/filerary/librarian.rb, line 40
def list
  @files.collect do |record|
    record._key
  end
end
remove(path) click to toggle source
# File lib/filerary/librarian.rb, line 93
def remove(path)
  raise ArgumentError, "file not found" unless @files[path]
  @files.delete(path)
end
show(path) click to toggle source
# File lib/filerary/librarian.rb, line 76
def show(path)
  file = @files[path]
  raise ArgumentError, "file not found" unless file
  file.content
end
size() click to toggle source
# File lib/filerary/librarian.rb, line 36
def size
  @files.size
end
update() click to toggle source
# File lib/filerary/librarian.rb, line 82
def update
  collect(list)
end

Private Instance Methods

default_base_dir() click to toggle source
# File lib/filerary/librarian.rb, line 104
def default_base_dir
  File.join(File.expand_path("~"), ".filerary")
end
read_content(path) click to toggle source
# File lib/filerary/librarian.rb, line 108
def read_content(path)
  text = nil

  ChupaText::Decomposers.load

  extractor = ChupaText::Extractor.new
  extractor.apply_configuration(ChupaText::Configuration.default)

  extractor.extract(path) do |text_data|
    text = text_data.body
  end

  return unless text

  # TODO: workaround for multibyte text for chupa-text gem.
  text.force_encoding(Encoding.default_external)
  text.force_encoding("UTF-8") unless text.valid_encoding?

  text
end