Module: DigestedFile

Included in:
FindingAidFile, SchematronFile
Defined in:
app/models/digested_file.rb

Overview

Module with common functionality for files represented by digests

A class including DigestedFile is expected to be a subclass of SimpleDelegator, and to have defined the constants FILE_DIR and INSPECT_SLUG prior to including the module, where:

  • FILE_DIR is the directory that files for this class will be stored in

  • INSPECT_SLUG is a string prepended to File in the console description of the object

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (String) digest (readonly)

Definitions for instance methods in including class

Returns:

  • (String)

    the SHA256 digest of the file's content in hex format



20
21
22
# File 'app/models/digested_file.rb', line 20

def digest
  @digest
end

Instance Method Details

- (DigestedFile) initialize(obj)

Looks up existing file of base type from filesystem or else creates a file

Parameters:

  • obj (String, #read)

    file or file contents

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/digested_file.rb', line 26

def initialize(obj)
  content = if obj.respond_to?(:read)
              obj.read
            else
              obj
            end

  @digest = Digest::SHA256.hexdigest(content)

  @fname = File.join(self.class::FILE_DIR, "#{@digest}.xml")

  File.open(@fname, 'w', 0444) do |f|
    f.write(content)
  end unless File.exist? @fname

  @delegation_target = File.new(@fname, 'r')

  super(@delegation_target)
end