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
- #destroy ⇒ Object
-
#initialize(base_dir = default_base_dir) ⇒ Librarian
constructor
A new instance of Librarian.
- #list ⇒ Object
- #remove(path) ⇒ Object
- #search(word) ⇒ Object
- #show(path) ⇒ Object
- #size ⇒ Object
- #update ⇒ 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
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.(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 ⇒ Object
98 99 100 101 |
# File 'lib/filerary/librarian.rb', line 98 def destroy FileUtils.rm(Dir.glob("#{@db_path}*")) FileUtils.rmdir(@db_dir) end |
#list ⇒ Object
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
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
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 |
#size ⇒ Object
36 37 38 |
# File 'lib/filerary/librarian.rb', line 36 def size @files.size end |
#update ⇒ Object
82 83 84 |
# File 'lib/filerary/librarian.rb', line 82 def update collect(list) end |