Class: Module

Inherits:
Object
  • Object
show all
Defined in:
lib/safe_monkeypatch.rb

Instance Method Summary collapse

Instance Method Details

#safe_monkeypatch(meth, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/safe_monkeypatch.rb', line 18

def safe_monkeypatch(meth, options={})
  options = options.dup
  info = options.delete(:error)

  if meth.is_a? UnboundMethod
    method = meth
    meth = method.name
  else
    begin
      method = instance_method(meth)
    rescue NameError => e
      raise SafeMonkeypatch::InvalidMethod, "#{inspect} has no method #{meth} anymore"
    end
  end

  source = method.source

  if options.empty?
    raise SafeMonkeypatch::ConfigurationError, "Provide at least one cypher name like: md5: '...'"
  end

  options.each do |cypher_name, expected|
    cypher = Digest.const_get(cypher_name.upcase)
    if (actual = cypher.hexdigest(source)) != expected
      raise SafeMonkeypatch::UpstreamChanged, "#{inspect}##{meth} expected to have #{cypher_name} expected: '#{expected}', but has: '#{actual}'\n#{info}".strip
    end
  end
end