Module: Contracts

Defined in:
lib/builtin_contracts.rb,
lib/testable.rb,
lib/contracts.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

Classes: And, Any, Args, ArrayOf, Bool, CallableClass, Func, IsA, Maybe, Neg, None, Not, Num, Or, Pos, RespondTo, Send, Testable, Xor

Class Method Summary collapse

Class Method Details

.common(base) ⇒ Object



16
17
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
# File 'lib/contracts.rb', line 16

def self.common base
  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}"
      end
    end
  end
  base.class_eval do
    def Contract(*args)
      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}"
      end
    end
  end
end

.extended(base) ⇒ Object



12
13
14
# File 'lib/contracts.rb', line 12

def self.extended(base)
  common base
end

.included(base) ⇒ Object



8
9
10
# File 'lib/contracts.rb', line 8

def self.included(base)
  common base
end