Class: Contract
- Inherits:
-
Contracts::Decorator
- Object
- Contracts::Decorator
- Contract
- 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
-
#args_contracts ⇒ Object
readonly
Returns the value of attribute args_contracts.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#ret_contract ⇒ Object
readonly
Returns the value of attribute ret_contract.
Class Method Summary collapse
-
.valid?(arg, contract) ⇒ Boolean
Used to verify if an argument satisfies a contract.
Instance Method Summary collapse
- #[](*args, &blk) ⇒ Object
- #call(*args, &blk) ⇒ Object
-
#failure_exception ⇒ Object
Used to determine type of failure exception this contract should raise in case of failure.
-
#initialize(klass, method, *contracts) ⇒ Contract
constructor
A new instance of Contract.
-
#pattern_match! ⇒ Object
mark contract as pattern matching contract.
-
#pattern_match? ⇒ Boolean
Used to determine if contract is a pattern matching contract.
- #to_s ⇒ Object
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
Methods inherited from Contracts::Decorator
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! @pattern_match = false @klass = klass @method = method end |
Instance Attribute Details
#args_contracts ⇒ Object (readonly)
Returns the value of attribute args_contracts.
23 24 25 |
# File 'lib/contracts/contract.rb', line 23 def args_contracts @args_contracts end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
23 24 25 |
# File 'lib/contracts/contract.rb', line 23 def klass @klass end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
23 24 25 |
# File 'lib/contracts/contract.rb', line 23 def method @method end |
#ret_contract ⇒ Object (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.
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_exception ⇒ Object
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
57 58 59 |
# File 'lib/contracts/contract.rb', line 57 def pattern_match? @pattern_match end |
#to_s ⇒ Object
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 |