Class: Stannum::Contract
- Inherits:
-
Stannum::Contracts::Base
- Object
- Stannum::Constraints::Base
- Stannum::Contracts::Base
- Stannum::Contract
- Defined in:
- lib/stannum/contract.rb
Overview
A Contract defines constraints on an object and its properties.
Direct Known Subclasses
Stannum::Contracts::MapContract, Stannum::Contracts::TupleContract
Defined Under Namespace
Classes: Builder
Constant Summary
Constants inherited from Stannum::Constraints::Base
Stannum::Constraints::Base::NEGATED_TYPE, Stannum::Constraints::Base::TYPE
Instance Attribute Summary
Attributes inherited from Stannum::Constraints::Base
Instance Method Summary collapse
-
#add_constraint(constraint, property: nil, sanity: false, **options) ⇒ self
Adds a constraint to the contract.
-
#add_property_constraint(property, constraint, sanity: false, **options) ⇒ self
Adds a property constraint to the contract.
Methods inherited from Stannum::Contracts::Base
#==, #concat, #does_not_match?, #each_constraint, #each_pair, #errors_for, #initialize, #match, #matches?, #negated_errors_for, #negated_match
Methods inherited from Stannum::Constraints::Base
#==, #clone, #does_not_match?, #dup, #errors_for, #initialize, #match, #matches?, #message, #negated_errors_for, #negated_match, #negated_message, #negated_type, #type, #with_options
Constructor Details
This class inherits a constructor from Stannum::Contracts::Base
Instance Method Details
#add_constraint(constraint, property: nil, sanity: false, **options) ⇒ self
Adds a constraint to the contract.
When the contract is matched with an object, the constraint will be evaluated with the object and the errors updated accordingly.
If the :property option is set, this defines a property constraint. See #add_property_constraint for more information.
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/stannum/contract.rb', line 139 def add_constraint(constraint, property: nil, sanity: false, **) validate_constraint(constraint) validate_property(property: property, **) @constraints << Stannum::Contracts::Definition.new( constraint: constraint, contract: self, options: .merge(property: property, sanity: sanity) ) self end |
#add_property_constraint(property, constraint, sanity: false, **options) ⇒ self
Adds a property constraint to the contract.
When the contract is called, the contract will find the value of that property for the given object. If the property is an array, the contract will recursively retrieve each property.
A property of nil will match against the given object itself, rather than one of its properties.
If the value does not match the constraint, then the error from the constraint will be added in an error namespace matching the constraint. For example, a property of :name will add the error message to errors.dig(:name), while a property of [:manufacturer, :address, :street] will add the error message to errors.dig(:manufacturer, :address, :street).
181 182 183 |
# File 'lib/stannum/contract.rb', line 181 def add_property_constraint(property, constraint, sanity: false, **) add_constraint(constraint, property: property, sanity: sanity, **) end |