Class: Snoopit::FileTracker
- Inherits:
-
Object
- Object
- Snoopit::FileTracker
- Defined in:
- lib/snoopit/file_tracker.rb
Instance Attribute Summary collapse
-
#db_file ⇒ Object
Returns the value of attribute db_file.
-
#files ⇒ Object
Returns the value of attribute files.
Instance Method Summary collapse
- #as_json ⇒ Object
- #foreach(file, &block) ⇒ Object
- #get_file(file) ⇒ Object
-
#initialize(db_file = './snooper_db.json', remove = false) ⇒ FileTracker
constructor
A new instance of FileTracker.
- #load_db(db_file) ⇒ Object
- #read_lines(file_info, block) ⇒ Object
- #remove_db(db_file) ⇒ Object
- #save_db(db_file) ⇒ Object
- #to_json(*args) ⇒ Object
Constructor Details
#initialize(db_file = './snooper_db.json', remove = false) ⇒ FileTracker
Returns a new instance of FileTracker.
6 7 8 9 10 11 12 13 14 |
# File 'lib/snoopit/file_tracker.rb', line 6 def initialize(db_file='./snooper_db.json', remove=false) @files = { } @db_file = db_file if remove remove_db @db_file else load_db @db_file end end |
Instance Attribute Details
#db_file ⇒ Object
Returns the value of attribute db_file.
4 5 6 |
# File 'lib/snoopit/file_tracker.rb', line 4 def db_file @db_file end |
#files ⇒ Object
Returns the value of attribute files.
4 5 6 |
# File 'lib/snoopit/file_tracker.rb', line 4 def files @files end |
Instance Method Details
#as_json ⇒ Object
74 75 76 |
# File 'lib/snoopit/file_tracker.rb', line 74 def as_json(*) { files: @files } end |
#foreach(file, &block) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/snoopit/file_tracker.rb', line 16 def foreach(file, &block) file_info = get_file(file) unless file_info.nil? read_lines(file_info, block) save_db @db_file end end |
#get_file(file) ⇒ Object
24 25 26 27 28 |
# File 'lib/snoopit/file_tracker.rb', line 24 def get_file(file) return nil unless File.exist? file @files[file] = FileInfo.new(file) if @files[file].nil? @files[file] end |
#load_db(db_file) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/snoopit/file_tracker.rb', line 46 def load_db(db_file) if (! db_file.nil?) && (File.exist? db_file) Snoopit.logger.debug 'Loading from db file: ' + db_file hash_db = JSON.parse(IO.read db_file) hash_db.each do |key, file_info| unless file_info.nil? fi = FileInfo.new fi.from_hash(file_info) @files[key] = fi end end end end |
#read_lines(file_info, block) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/snoopit/file_tracker.rb', line 30 def read_lines(file_info, block) begin fh = File.new(file_info.file) if file_info.updated? fh fh.each_line do |line| file_info.offset += line.bytesize file_info.line_no += 1 file_info.last_line = line block.call line, file_info.line_no end end ensure fh.close end end |
#remove_db(db_file) ⇒ Object
68 69 70 71 72 |
# File 'lib/snoopit/file_tracker.rb', line 68 def remove_db(db_file) if (! db_file.nil?) && (File.exist? db_file) File.delete db_file end end |
#save_db(db_file) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/snoopit/file_tracker.rb', line 60 def save_db(db_file) unless db_file.nil? File.open db_file, 'w' do |f| f.write @files.to_json end end end |
#to_json(*args) ⇒ Object
78 79 80 |
# File 'lib/snoopit/file_tracker.rb', line 78 def to_json(*args) as_json.to_json(*args) end |