Class: TTY::File::DigestFile

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/file/digest_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, mode) ⇒ DigestFile

Returns a new instance of DigestFile.



11
12
13
14
# File 'lib/tty/file/digest_file.rb', line 11

def initialize(source, mode)
  @source = source
  @digest = OpenSSL::Digest.new(mode)
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



9
10
11
# File 'lib/tty/file/digest_file.rb', line 9

def source
  @source
end

Instance Method Details

#callObject



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tty/file/digest_file.rb', line 16

def call
  if ::FileTest.file?(source.to_s)
    ::File.open(source, "rb") { |f| checksum_io(f, @digest) }
  else
    non_file = source
    if non_file.is_a?(String)
      non_file = StringIO.new(non_file)
    end
    if non_file.is_a?(StringIO)
      checksum_io(non_file, @digest)
    end
  end
end

#checksum_io(io, digest) ⇒ Object



30
31
32
33
34
35
# File 'lib/tty/file/digest_file.rb', line 30

def checksum_io(io, digest)
  while (chunk = io.read(1024 * 8))
    digest << chunk
  end
  digest.hexdigest
end