Class: Stannum::Contracts::Parameters::KeywordsContract Private

Inherits:
IndifferentHashContract show all
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

#options

Instance Method Summary collapse

Methods inherited from HashContract

#key_type, #value_type

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.

Parameters:

  • options (Hash<Symbol, Object>)

    Configuration options for the contract. Defaults to an empty Hash.



17
18
19
20
21
22
# File 'lib/stannum/contracts/parameters/keywords_contract.rb', line 17

def initialize(**options)
  super(
    allow_extra_keys: false,
    **options
  )
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.

Parameters:

  • keyword (Symbol)

    The keyword to constrain.

  • type (Class, Module, Stannum::Constraints:Base)

    The expected type of the argument.

  • default (Boolean) (defaults to: false)

    If true, the keyword has a default value, and the constraint will ignore keywords with no value at that key.

  • options (Hash<Symbol, Object>)

    Configuration options for the constraint. Defaults to an empty Hash.

Returns:



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, **options)
  unless keyword.is_a?(Symbol)
    raise ArgumentError, 'keyword must be a symbol'
  end

  constraint = Stannum::Support::Coercion.type_constraint(type, **options)

  add_key_constraint(keyword, constraint, default: !!default, **options)

  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.

Parameters:

  • constraint (Stannum::Constraints::Base)

    The constraint to add. The variadic keywords (a hash) as a whole must match the given constraint.

  • as (Symbol) (defaults to: nil)

    A human-friendly reference for the additional keywords. Used when generating errors. Should be the same name used in the method definition.

Returns:

  • (self)

    the contract.

Raises:

  • (RuntimeError)

    if the variadic keywords constraint is already set.

See Also:

  • #set_variadic_item_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?

  options[:allow_extra_keys] = true

  variadic_constraint.receiver = constraint

  variadic_definition.options[: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.

Parameters:

  • value_type (Stannum::Constraints::Base, Class, Module)

    The type or constraint to add. If the type is a Class or Module, then it is converted to a Stannum::Constraints::Type. Each value in the variadic keywords must match the given constraint.

  • as (Symbol) (defaults to: nil)

    A human-friendly reference for the additional keywords. Used when generating errors. Should be the same name used in the method definition.

Returns:

  • (self)

    the contract.

Raises:

  • (RuntimeError)

    if the variadic keywords constraint is already set.

See Also:



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