Class: Stannum::Contracts::Parameters::KeywordsContract Private
- Inherits:
-
IndifferentHashContract
- Object
- Stannum::Constraints::Base
- Base
- Stannum::Contract
- MapContract
- HashContract
- IndifferentHashContract
- Stannum::Contracts::Parameters::KeywordsContract
- Defined in:
- lib/stannum/contracts/parameters/keywords_contract.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
A KeywordsContract constrains the keywords given for a method.
Constant Summary collapse
- UNDEFINED =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Value used when keywords hash does not have a value for the given key.
Object.new.freeze
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_keyword_constraint(keyword, type, default: false, **options) ⇒ Stannum::Contracts::Parameters::KeywordsContract
private
Adds a keyword constraint to the contract.
-
#initialize(**options) ⇒ KeywordsContract
constructor
private
A new instance of KeywordsContract.
-
#set_variadic_constraint(constraint, as: nil) ⇒ self
private
Sets a constraint for the variadic keywords.
-
#set_variadic_value_constraint(value_type, as: nil) ⇒ self
private
Sets a constraint for the variadic keyword values.
Methods inherited from HashContract
Methods inherited from MapContract
#add_key_constraint, #allow_extra_keys?, #expected_keys, #with_options
Methods inherited from Stannum::Contract
#add_constraint, #add_property_constraint
Methods inherited from Base
#==, #add_constraint, #concat, #does_not_match?, #each_constraint, #each_pair, #errors_for, #match, #matches?, #negated_errors_for, #negated_match
Methods inherited from Stannum::Constraints::Base
#==, #clone, #does_not_match?, #dup, #errors_for, #match, #matches?, #message, #negated_errors_for, #negated_match, #negated_message, #negated_type, #type, #with_options
Constructor Details
#initialize(**options) ⇒ KeywordsContract
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of KeywordsContract.
17 18 19 20 21 22 |
# File 'lib/stannum/contracts/parameters/keywords_contract.rb', line 17 def initialize(**) super( allow_extra_keys: false, ** ) end |
Instance Method Details
#add_keyword_constraint(keyword, type, default: false, **options) ⇒ Stannum::Contracts::Parameters::KeywordsContract
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds a keyword constraint to the contract.
Generates a keyword constraint based on the given type. If the type is a constraint, then the given constraint will be copied with the given options and added for the given keyword. If the type is a Class or a Module, then a Stannum::Constraints::Type constraint will be created with the given type and options and added for the keyword.
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/stannum/contracts/parameters/keywords_contract.rb', line 41 def add_keyword_constraint(keyword, type, default: false, **) unless keyword.is_a?(Symbol) raise ArgumentError, 'keyword must be a symbol' end constraint = Stannum::Support::Coercion.type_constraint(type, **) add_key_constraint(keyword, constraint, default: !!default, **) self end |
#set_variadic_constraint(constraint, as: nil) ⇒ self
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Sets a constraint for the variadic keywords.
The given constraint must match the variadic keywords hash as a whole. To constraint each individual value, use #set_variadic_value_constraint.
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/stannum/contracts/parameters/keywords_contract.rb', line 70 def set_variadic_constraint(constraint, as: nil) raise 'variadic keywords constraint is already set' if allow_extra_keys? [:allow_extra_keys] = true variadic_constraint.receiver = constraint variadic_definition.[:property_name] = as if as self end |
#set_variadic_value_constraint(value_type, as: nil) ⇒ self
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Sets a constraint for the variadic keyword values.
The given type or constraint must individually match each value (if any) in the variadic keywords. To constrain the variadic keywords as a whole, use #set_variadic_constraint.
101 102 103 104 105 106 107 108 109 |
# File 'lib/stannum/contracts/parameters/keywords_contract.rb', line 101 def set_variadic_value_constraint(value_type, as: nil) type = coerce_value_type(value_type) constraint = Stannum::Constraints::Types::HashType.new( key_type: Stannum::Constraints::Types::SymbolType.new, value_type: type ) set_variadic_constraint(constraint, as: as) end |