Module: Contracts

Defined in:
lib/contracts/builtin_contracts.rb,
lib/contracts.rb,
lib/contracts/engine.rb,
lib/contracts/errors.rb,
lib/contracts/support.rb,
lib/contracts/version.rb,
lib/contracts/call_with.rb,
lib/contracts/decorators.rb,
lib/contracts/formatters.rb,
lib/contracts/invariants.rb,
lib/contracts/validators.rb,
lib/contracts/engine/base.rb,
lib/contracts/engine/target.rb,
lib/contracts/method_handler.rb,
lib/contracts/method_reference.rb,
lib/contracts/engine/eigenclass.rb

Overview

rdoc 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: CallWith, Engine, Formatters, Invariants, MethodDecorators, Support, Validators Classes: And, Any, Args, Bool, CallableClass, CollectionOf, ContractsNotIncluded, Decorator, Eq, Exactly, Func, HashOf, KeywordArgs, Maybe, MethodHandler, MethodReference, Nat, Neg, None, Not, Num, Optional, Or, Pos, RangeOf, RespondTo, Send, SingletonMethodReference, Xor

Constant Summary collapse

VERSION =
"0.10"
ArrayOf =

Takes a contract. The related argument must be an array. Checks the contract against every element of the array. If it passes for all elements, the contract passes. Example: ArrayOf[Num]

CollectionOf::Factory.new(Array)
SetOf =

Takes a contract. The related argument must be a set. Checks the contract against every element of the set. If it passes for all elements, the contract passes. Example: SetOf[Num]

CollectionOf::Factory.new(Set)

Class Method Summary collapse

Class Method Details

.common(base) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/contracts.rb', line 22

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

  base.extend(MethodDecorators)

  base.instance_eval do
    def functype(funcname)
      contracts = Engine.fetch_from(self).decorated_methods_for(:class_methods, funcname)
      if contracts.nil?
        "No contract for #{self}.#{funcname}"
      else
        "#{funcname} :: #{contracts[0]}"
      end
    end
  end

  base.class_eval do
    # TODO: deprecate
    # Required when contracts are included in global scope
    def Contract(*args)
      self.class.Contract(*args)
    end

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

.extended(base) ⇒ Object



18
19
20
# File 'lib/contracts.rb', line 18

def self.extended(base)
  common(base)
end

.included(base) ⇒ Object



14
15
16
# File 'lib/contracts.rb', line 14

def self.included(base)
  common(base)
end