Class: Prependers::Annotate::Verify

Inherits:
Module
  • Object
show all
Defined in:
lib/prependers/annotate/verify.rb

Constant Summary collapse

WRONG_HASH_ERROR =
"The stored hash for %{prepended_module} in %{prepender} is %{stored_hash}, but the " \
"current hash is %{current_hash} instead.\n\n" \
"This most likely means that the original source has changed.\n\n" \
"Check that your prepender is still valid, then update the stored hash:\n\n" \
"    include Prependers::Prepender[verify: '%{current_hash}']"
UNSET_HASH_ERROR =
"You have not defined an original hash for %{prepended_module} in %{prepender}.\n\n" \
"You can define the hash by updating your include statement as follows:\n\n" \
"    include Prependers::Prepender[verify: '%{current_hash}']"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_hash) ⇒ Verify

Returns a new instance of Verify.



24
25
26
# File 'lib/prependers/annotate/verify.rb', line 24

def initialize(original_hash)
  @original_hash = original_hash
end

Instance Attribute Details

#original_hashObject (readonly)

Returns the value of attribute original_hash.



18
19
20
# File 'lib/prependers/annotate/verify.rb', line 18

def original_hash
  @original_hash
end

Class Method Details

.[](original_hash) ⇒ Object



20
21
22
# File 'lib/prependers/annotate/verify.rb', line 20

def self.[](original_hash)
  new(original_hash)
end

Instance Method Details

#included(prepender) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/prependers/annotate/verify.rb', line 28

def included(prepender)
  prependable = Prependers.prependable_for(prepender)
  current_hash = compute_hash(prependable)

  return if current_hash == original_hash

  error = (original_hash ? WRONG_HASH_ERROR : UNSET_HASH_ERROR) % {
    prepended_module: prependable,
    prepender: prepender.name,
    stored_hash: original_hash,
    current_hash: current_hash,
  }

  raise OutdatedPrependerError, error
end