class Prependers::Annotate::Verify

Constants

UNSET_HASH_ERROR
WRONG_HASH_ERROR

Attributes

original_hash[R]

Public Class Methods

[](original_hash) click to toggle source
# File lib/prependers/annotate/verify.rb, line 20
def self.[](original_hash)
  new(original_hash)
end
new(original_hash) click to toggle source
# File lib/prependers/annotate/verify.rb, line 24
def initialize(original_hash)
  @original_hash = original_hash
end

Public Instance Methods

included(prepender) click to toggle source
# 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

Private Instance Methods

compute_hash(constant) click to toggle source
# File lib/prependers/annotate/verify.rb, line 46
def compute_hash(constant)
  methods = constant.methods(false).map(&constant.method(:method)) +
            constant.instance_methods(false).map(&constant.method(:instance_method))

  raise NameError, "#{constant} has no methods" if methods.empty?

  source_locations = methods.map(&:source_location).compact.map(&:first).uniq.sort

  contents = source_locations.map { |path| File.read(path) }.join

  Digest::SHA1.hexdigest(contents)
end