module Aquarium::Extras::DesignByContract

A simple Design by Contract module. Adds advice to test that the contract, which is specified with a block passes. Note that it doesn’t attempt to handle the correct behavior under contract inheritance. A usage example is included in the Examples as part of the distribution and it is also shown on the web site. Normally, you want to disable the contracts in production runs, so you avoid the overhead. To do this effectively, call ::disable_all before any contracts are created. That will prevent all of the aspects from being created along with their overhead. Warning: This module automatically includes Aquarium::DSL into the class with the contract and it adds the :precondition, :postcondition, and the :invariant methods to Object!

Public Class Methods

disable_all() click to toggle source

Disable creation of any subsequent contracts and disable execution of existing contracts. That is, while contracts are disabled, it no existing contracts will be executed and any attempts to define new contracts will be ignored.

# File lib/aquarium/extras/design_by_contract.rb, line 36
def self.disable_all
  @@enabled = false
end
enable_all() click to toggle source

Enable creation and execution of contracts

# File lib/aquarium/extras/design_by_contract.rb, line 29
def self.enable_all
  @@enabled = true
end

Public Instance Methods

invariant(*args, &contract_block) click to toggle source
# File lib/aquarium/extras/design_by_contract.rb, line 52
def invariant *args, &contract_block
  return unless @@enabled
  message = handle_message_arg args
  Aspect.new make_args(:around, *args) do |jp, obj, *params|
    DesignByContract.test_condition "invariant failure (before invocation): #{message}", jp, obj, *params, &contract_block
    result = jp.proceed
    DesignByContract.test_condition "invariant failure (after invocation): #{message}", jp, obj, *params, &contract_block
    result
  end
end
postcondition(*args, &contract_block) click to toggle source
# File lib/aquarium/extras/design_by_contract.rb, line 46
def postcondition *args, &contract_block
  return unless @@enabled
  message = handle_message_arg args
  add_advice :after_returning, "postcondition", message, *args, &contract_block
end
precondition(*args, &contract_block) click to toggle source
# File lib/aquarium/extras/design_by_contract.rb, line 40
def precondition *args, &contract_block
  return unless @@enabled
  message = handle_message_arg args
  add_advice :before, "precondition", message, *args, &contract_block
end