Module: Contracts

Defined in:
lib/contracts/builtin_contracts.rb,
lib/contracts.rb,
lib/contracts/support.rb,
lib/contracts/version.rb,
lib/contracts/testable.rb,
lib/contracts/decorators.rb,
lib/contracts/invariants.rb

Overview

This module contains all the builtin contracts. If you want to use them, first:

import Contracts

And then use these or write your own!

A simple example:

Contract Num, Num => Num
def add(a, b)
   a + b
 end

The contract is Contract Num, Num, Num. That says that the add function takes two numbers and returns a number.

Defined Under Namespace

Modules: Invariants, MethodDecorators, Support Classes: And, Any, Args, ArrayOf, Bool, CallableClass, Decorator, Exactly, Func, HashOf, Maybe, Neg, None, Not, Num, Or, Pos, RespondTo, Send, Testable, Xor

Constant Summary collapse

VERSION =
"0.5"

Class Method Summary collapse

Class Method Details

.common(base) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/contracts.rb', line 48

def self.common base
  return if base.respond_to?(:Contract)

  base.extend MethodDecorators
  base.instance_eval do
    def functype(funcname)
      contracts = self.decorated_methods[:class_methods][funcname]
      if contracts.nil?
        "No contract for #{self}.#{funcname}"
      else
        "#{funcname} :: #{contracts[0]}"
      end
    end
  end
  base.class_eval do
    def Contract(*args)
      return if ENV["NO_CONTRACTS"]
      self.class.Contract(*args)
    end

    def functype(funcname)
      contracts = self.class.decorated_methods[:instance_methods][funcname]
      if contracts.nil?
        "No contract for #{self.class}.#{funcname}"
      else
        "#{funcname} :: #{contracts[0]}"
      end
    end
  end
end

.extended(base) ⇒ Object



44
45
46
# File 'lib/contracts.rb', line 44

def self.extended(base)
  common base
end

.included(base) ⇒ Object



40
41
42
# File 'lib/contracts.rb', line 40

def self.included(base)
  common base
end