module Contracts::FailureCallback

Constants

DEFAULT_FAILURE_CALLBACK

Default implementation of failure_callback. Provided as a block to be able to monkey patch failure_callback only temporary and then switch it back. First important usage - for specs.

Public Instance Methods

failure_callback(data, use_pattern_matching = true) click to toggle source

Callback for when a contract fails. By default it raises an error and prints detailed info about the contract that failed. You can also monkeypatch this callback to do whatever you want…log the error, send you an email, print an error message, etc.

Example of monkeypatching:

def Contract.failure_callback(data)
  puts "You had an error!"
  puts failure_msg(data)
  exit
end
# File lib/contracts/contract/failure_callback.rb, line 29
def failure_callback(data, use_pattern_matching = true)
  if data[:contracts].pattern_match? && use_pattern_matching
    return DEFAULT_FAILURE_CALLBACK.call(data)
  end

  fetch_failure_callback.call(data)
end
fetch_failure_callback() click to toggle source
# File lib/contracts/contract/failure_callback.rb, line 57
def fetch_failure_callback
  @failure_callback ||= DEFAULT_FAILURE_CALLBACK
end
override_failure_callback(&blk) click to toggle source

Used to override failure_callback without monkeypatching.

Takes: block parameter, that should accept one argument - data.

Example usage:

Contract.override_failure_callback do |data|
  puts "You had an error"
  puts failure_msg(data)
  exit
end
# File lib/contracts/contract/failure_callback.rb, line 48
def override_failure_callback(&blk)
  @failure_callback = blk
end
restore_failure_callback() click to toggle source

Used to restore default failure callback

# File lib/contracts/contract/failure_callback.rb, line 53
def restore_failure_callback
  @failure_callback = DEFAULT_FAILURE_CALLBACK
end