Project

contract

0.0
No commit activity in last 3 years
No release in over 3 years
Contract helper
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
 Project Readme

Contract helper

This module provides a way to define contracts in functions, and return a value based on a series of user defined verifications.

Usage

The functions of a contract can be used directly from the Contract module:

class Foo
  def foo(a, b)
    Contract.contract do
      Contract.verify(Numeric === a)
      Contract.verify(Numeric === b)

      a * b
    end
  end
end

assert_equal 6,     Foo.new.foo(2, 3)
assert_equal false, Foo.new.foo(2, "3")

When the Contract module is included, the methods contract and verify become available.

class Bar
  include Contract

  def bar(a, b)
    contract do
      verify(Numeric === a)
      verify(Numeric === b)

      a * b
    end
  end
end

assert_equal 6,     Bar.new.bar(2, 3)
assert_equal false, Bar.new.bar(2, "3")

Installation

  $ gem install contract