Class: Bundler::Checksum
- Inherits:
-
Object
- Object
- Bundler::Checksum
- Defined in:
- lib/bundler/checksum.rb
Defined Under Namespace
Constant Summary collapse
- ALGO_SEPARATOR =
"="
Instance Attribute Summary collapse
-
#algo ⇒ Object
readonly
Returns the value of attribute algo.
-
#digest ⇒ Object
readonly
Returns the value of attribute digest.
-
#sources ⇒ Object
readonly
Returns the value of attribute sources.
Class Method Summary collapse
- .from_api(digest, source_uri, algo = DEFAULT_ALGORITHM) ⇒ Object
- .from_gem(io, pathname, algo = DEFAULT_ALGORITHM) ⇒ Object
- .from_gem_package(gem_package, algo = DEFAULT_ALGORITHM) ⇒ Object
- .from_lock(lock_checksum, lockfile_location) ⇒ Object
- .to_hexdigest(digest, algo = DEFAULT_ALGORITHM) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #formatted_sources ⇒ Object
- #hash ⇒ Object
-
#initialize(algo, digest, source) ⇒ Checksum
constructor
A new instance of Checksum.
- #inspect ⇒ Object
- #match?(other) ⇒ Boolean
- #merge!(other) ⇒ Object
- #removable? ⇒ Boolean
- #removal_instructions ⇒ Object
- #same_source?(other) ⇒ Boolean
- #to_lock ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(algo, digest, source) ⇒ Checksum
Returns a new instance of Checksum.
57 58 59 60 61 |
# File 'lib/bundler/checksum.rb', line 57 def initialize(algo, digest, source) @algo = algo @digest = digest @sources = [source] end |
Instance Attribute Details
#algo ⇒ Object (readonly)
Returns the value of attribute algo.
55 56 57 |
# File 'lib/bundler/checksum.rb', line 55 def algo @algo end |
#digest ⇒ Object (readonly)
Returns the value of attribute digest.
55 56 57 |
# File 'lib/bundler/checksum.rb', line 55 def digest @digest end |
#sources ⇒ Object (readonly)
Returns the value of attribute sources.
55 56 57 |
# File 'lib/bundler/checksum.rb', line 55 def sources @sources end |
Class Method Details
.from_api(digest, source_uri, algo = DEFAULT_ALGORITHM) ⇒ Object
31 32 33 34 35 |
# File 'lib/bundler/checksum.rb', line 31 def from_api(digest, source_uri, algo = DEFAULT_ALGORITHM) return if Bundler.settings[:disable_checksum_validation] Checksum.new(algo, to_hexdigest(digest, algo), Source.new(:api, source_uri)) end |
.from_gem(io, pathname, algo = DEFAULT_ALGORITHM) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/bundler/checksum.rb', line 24 def from_gem(io, pathname, algo = DEFAULT_ALGORITHM) digest = Bundler::SharedHelpers.digest(algo.upcase).new buf = String.new(capacity: DEFAULT_BLOCK_SIZE) digest << io.readpartial(DEFAULT_BLOCK_SIZE, buf) until io.eof? Checksum.new(algo, digest.hexdigest!, Source.new(:gem, pathname)) end |
.from_gem_package(gem_package, algo = DEFAULT_ALGORITHM) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/bundler/checksum.rb', line 12 def from_gem_package(gem_package, algo = DEFAULT_ALGORITHM) return if Bundler.settings[:disable_checksum_validation] return unless source = gem_package.instance_variable_get(:@gem) return unless source.respond_to?(:with_read_io) source.with_read_io do |io| from_gem(io, source.path) ensure io.rewind end end |
.from_lock(lock_checksum, lockfile_location) ⇒ Object
37 38 39 40 |
# File 'lib/bundler/checksum.rb', line 37 def from_lock(lock_checksum, lockfile_location) algo, digest = lock_checksum.strip.split(ALGO_SEPARATOR, 2) Checksum.new(algo, to_hexdigest(digest, algo), Source.new(:lock, lockfile_location)) end |
.to_hexdigest(digest, algo = DEFAULT_ALGORITHM) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/bundler/checksum.rb', line 42 def to_hexdigest(digest, algo = DEFAULT_ALGORITHM) return digest unless algo == DEFAULT_ALGORITHM return digest if digest.match?(/\A[0-9a-f]{64}\z/i) if digest.match?(%r{\A[-0-9a-z_+/]{43}={0,2}\z}i) digest = digest.tr("-_", "+/") # fix urlsafe base64 digest.unpack1("m0").unpack1("H*") else raise ArgumentError, "#{digest.inspect} is not a valid SHA256 hex or base64 digest" end end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
63 64 65 |
# File 'lib/bundler/checksum.rb', line 63 def ==(other) match?(other) && other.sources == sources end |
#formatted_sources ⇒ Object
96 97 98 |
# File 'lib/bundler/checksum.rb', line 96 def formatted_sources sources.join("\n and ").concat("\n") end |
#hash ⇒ Object
77 78 79 |
# File 'lib/bundler/checksum.rb', line 77 def hash digest.hash end |
#inspect ⇒ Object
114 115 116 117 118 |
# File 'lib/bundler/checksum.rb', line 114 def inspect abbr = "#{algo}#{ALGO_SEPARATOR}#{digest[0, 8]}" from = "from #{sources.join(" and ")}" "#<#{self.class}:#{object_id} #{abbr} #{from}>" end |
#match?(other) ⇒ Boolean
73 74 75 |
# File 'lib/bundler/checksum.rb', line 73 def match?(other) other.is_a?(self.class) && other.digest == digest && other.algo == algo end |
#merge!(other) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/bundler/checksum.rb', line 89 def merge!(other) return nil unless match?(other) @sources.concat(other.sources).uniq! self end |
#removable? ⇒ Boolean
100 101 102 |
# File 'lib/bundler/checksum.rb', line 100 def removable? sources.all?(&:removable?) end |
#removal_instructions ⇒ Object
104 105 106 107 108 109 110 111 112 |
# File 'lib/bundler/checksum.rb', line 104 def removal_instructions msg = +"" i = 1 sources.each do |source| msg << " #{i}. #{source.removal}\n" i += 1 end msg << " #{i}. run `bundle install`\n" end |
#same_source?(other) ⇒ Boolean
69 70 71 |
# File 'lib/bundler/checksum.rb', line 69 def same_source?(other) sources.include?(other.sources.first) end |
#to_lock ⇒ Object
85 86 87 |
# File 'lib/bundler/checksum.rb', line 85 def to_lock "#{algo}#{ALGO_SEPARATOR}#{digest}" end |
#to_s ⇒ Object
81 82 83 |
# File 'lib/bundler/checksum.rb', line 81 def to_s "#{to_lock} (from #{sources.first}#{", ..." if sources.size > 1})" end |