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.
41 42 43 44 |
# File 'lib/metanorma/release/content_hash.rb', line 41 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 32 33 34 35 36 37 38 39 |
# File 'lib/metanorma/release/content_hash.rb', line 27 def self.of_directory(directory, base: nil) pattern = if base File.join(directory, "#{base}.*") else File.join(directory, "**", "*") end files = Dir.glob(pattern).reject do |f| File.directory?(f) || f.end_with?(".zip") end 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
50 51 52 |
# File 'lib/metanorma/release/content_hash.rb', line 50 def eql?(other) other.is_a?(self.class) && @hex == other.to_s end |
#hash ⇒ Object
54 55 56 |
# File 'lib/metanorma/release/content_hash.rb', line 54 def hash @hex.hash end |
#to_s ⇒ Object
46 47 48 |
# File 'lib/metanorma/release/content_hash.rb', line 46 def to_s @hex end |