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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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

  found_match = false
  error_parts = ["#{inspect}##{meth} expected to have"]

  options.each do |cypher_name, expected|
    cypher = Digest.const_get(cypher_name.upcase)
    actual = cypher.hexdigest(source)
    found_match = if expected.is_a? Array
                    expected.any? { |e| e == actual }
                  else
                    actual == expected
                  end

    unless found_match
      error_parts << "#{cypher_name} expected: #{expected.inspect}, but has: #{actual.inspect}"
    end
  end

  error_parts << info.to_s if info

  if block_given? && found_match
    yield
  elsif block_given?
    nil # pass
  elsif not found_match
    raise SafeMonkeypatch::UpstreamChanged, error_parts.join(" ")
  end
end