Class: Document

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/statements/models/document.rb

Instance Method Summary collapse

Instance Method Details

#scan(base: nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/statements/models/document.rb', line 6

def scan(base: nil)
  path = base + self.path
  md5 = Digest::MD5.file(path).hexdigest.downcase
  print "Scanning #{self.path} ... "
  if md5 == self.md5
    puts 'skipping (unchanged)'
  else
    reader = Statements::Reader.for_file(path)
    if reader
      Transaction.delete_all document: self if persisted?
      reader.transactions.each do |t|
        t.document = self
        t.save! unless Transaction.find_by('checksum = ? AND document_id != ?', t.checksum!, id || 0)
      end
      puts "added #{transactions.count} transactions(s)"
    else
      puts 'skipping (unknown format)'
    end
  end
  self.md5 = md5
  save!
end