Class: Contract

Inherits:
Contracts::Decorator show all
Extended by:
Contracts::FailureCallback, Contracts::Validators
Includes:
Contracts::CallWith
Defined in:
lib/contracts/contract.rb

Overview

This is the main Contract class. When you write a new contract, you’ll write it as:

Contract [contract names] => return_value

This class also provides useful callbacks and a validation method.

Constant Summary

Constants included from Contracts::Validators

Contracts::Validators::DEFAULT_VALIDATOR_STRATEGIES

Constants included from Contracts::FailureCallback

Contracts::FailureCallback::DEFAULT_FAILURE_CALLBACK

Constants included from Contracts::CallWith

Contracts::CallWith::SILENT_FAILURE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Contracts::Validators

clean_memoized_validators, make_validator, make_validator!, memoized_validators, override_validator, reset_validators, restore_validators, validator_key, validator_strategies

Methods included from Contracts::FailureCallback

failure_callback, fetch_failure_callback, override_failure_callback, restore_failure_callback

Methods included from Contracts::CallWith

#call_with

Methods inherited from Contracts::Decorator

inherited

Constructor Details

#initialize(klass, method, *contracts) ⇒ Contract

Returns a new instance of Contract.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/contracts/contract.rb', line 24

def initialize(klass, method, *contracts)
  contracts = correct_ret_only_contract(contracts, method)

  # internally we just convert that return value syntax back to an array
  @args_contracts = contracts[0, contracts.size - 1] + contracts[-1].keys
  @ret_contract   = contracts[-1].values[0]

  determine_has_proc_contract!
  determine_has_options_contract!

  @pattern_match = false
  @klass         = klass
  @method        = method
end

Instance Attribute Details

#args_contractsObject (readonly)

Returns the value of attribute args_contracts.



23
24
25
# File 'lib/contracts/contract.rb', line 23

def args_contracts
  @args_contracts
end

#klassObject (readonly)

Returns the value of attribute klass.



23
24
25
# File 'lib/contracts/contract.rb', line 23

def klass
  @klass
end

#methodObject (readonly)

Returns the value of attribute method.



23
24
25
# File 'lib/contracts/contract.rb', line 23

def method
  @method
end

#ret_contractObject (readonly)

Returns the value of attribute ret_contract.



23
24
25
# File 'lib/contracts/contract.rb', line 23

def ret_contract
  @ret_contract
end

Class Method Details

.valid?(arg, contract) ⇒ Boolean

Used to verify if an argument satisfies a contract.

Takes: an argument and a contract.

Returns: a tuple: [Boolean, metadata]. The boolean indicates whether the contract was valid or not. If it wasn’t, metadata contains some useful information about the failure.

Returns:

  • (Boolean)


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

def self.valid?(arg, contract)
  make_validator(contract)[arg]
end

Instance Method Details

#[](*args, &blk) ⇒ Object



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

def [](*args, &blk)
  call(*args, &blk)
end

#call(*args, &blk) ⇒ Object



47
48
49
# File 'lib/contracts/contract.rb', line 47

def call(*args, &blk)
  call_with(nil, *args, &blk)
end

#failure_exceptionObject

Used to determine type of failure exception this contract should raise in case of failure



62
63
64
65
# File 'lib/contracts/contract.rb', line 62

def failure_exception
  return PatternMatchingError if pattern_match?
  ParamContractError
end

#pattern_match!Object

mark contract as pattern matching contract



52
53
54
# File 'lib/contracts/contract.rb', line 52

def pattern_match!
  @pattern_match = true
end

#pattern_match?Boolean

Used to determine if contract is a pattern matching contract

Returns:

  • (Boolean)


57
58
59
# File 'lib/contracts/contract.rb', line 57

def pattern_match?
  @pattern_match
end

#to_sObject



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

def to_s
  "#{args_contracts_to_s} => #{ret_contract_to_s}".gsub!("Contracts::Builtin::", "")
end