Class: Metanorma::Release::ContentHash

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/release/content_hash.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hex) ⇒ ContentHash

Returns a new instance of ContentHash.



33
34
35
36
# File 'lib/metanorma/release/content_hash.rb', line 33

def initialize(hex)
  @hex = hex
  freeze
end

Class Method Details

.from_hex(hex_string) ⇒ Object



8
9
10
# File 'lib/metanorma/release/content_hash.rb', line 8

def self.from_hex(hex_string)
  new(hex_string.to_s)
end

.of_content(data) ⇒ Object



12
13
14
# File 'lib/metanorma/release/content_hash.rb', line 12

def self.of_content(data)
  new(Digest::SHA256.hexdigest(data))
end

.of_directory(directory, base: nil) ⇒ Object



27
28
29
30
31
# File 'lib/metanorma/release/content_hash.rb', line 27

def self.of_directory(directory, base: nil)
  pattern = base ? File.join(directory, "#{base}.*") : File.join(directory, '**', '*')
  files = Dir.glob(pattern).reject { |f| File.directory?(f) || f.end_with?('.zip') }
  of_files(files)
end

.of_file(path) ⇒ Object



16
17
18
# File 'lib/metanorma/release/content_hash.rb', line 16

def self.of_file(path)
  new(Digest::SHA256.file(path).hexdigest)
end

.of_files(paths) ⇒ Object



20
21
22
23
24
25
# File 'lib/metanorma/release/content_hash.rb', line 20

def self.of_files(paths)
  sorted = paths.sort
  digest = Digest::SHA256.new
  sorted.each { |p| digest << File.binread(p) }
  new(digest.hexdigest)
end

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/metanorma/release/content_hash.rb', line 42

def eql?(other)
  other.is_a?(self.class) && @hex == other.to_s
end

#hashObject



46
47
48
# File 'lib/metanorma/release/content_hash.rb', line 46

def hash
  @hex.hash
end

#to_sObject



38
39
40
# File 'lib/metanorma/release/content_hash.rb', line 38

def to_s
  @hex
end