Class: Metanorma::Release::ContentHash
- Inherits:
-
Object
- Object
- Metanorma::Release::ContentHash
- Defined in:
- lib/metanorma/release/content_hash.rb
Class Method Summary collapse
- .from_hex(hex_string) ⇒ Object
- .of_content(data) ⇒ Object
- .of_directory(directory, base: nil) ⇒ Object
- .of_file(path) ⇒ Object
- .of_files(paths) ⇒ Object
Instance Method Summary collapse
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(hex) ⇒ ContentHash
constructor
A new instance of ContentHash.
- #to_s ⇒ Object
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
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 |
#hash ⇒ Object
46 47 48 |
# File 'lib/metanorma/release/content_hash.rb', line 46 def hash @hex.hash end |
#to_s ⇒ Object
38 39 40 |
# File 'lib/metanorma/release/content_hash.rb', line 38 def to_s @hex end |