Class: Stannum::Contracts::ParametersContract::Builder Private

Inherits:
Stannum::Contract::Builder show all
Defined in:
lib/stannum/contracts/parameters_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.

Builder class for defining item constraints for a ParametersContract.

This class should not be invoked directly. Instead, pass a block to the constructor for ParametersContract.

Instance Method Summary collapse

Methods inherited from Stannum::Contract::Builder

#property

Instance Method Details

#argument(name, type, index: nil, **options) ⇒ Stannum::Contracts::ParametersContract::Builder #argument(name, index: nil, **options) {|actual| ... } ⇒ Stannum::Contracts::ParametersContract::Builder

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 an argument constraint to the contract.

If the index is specified, then the constraint will be added for the argument at the specified index. If the index is not given, then the constraint will be applied to the next unconstrained argument. For example, the first argument constraint will be added for the argument at index 0, the second constraint for the argument at index 1, and so on.

Overloads:

  • #argument(name, type, index: nil, **options) ⇒ Stannum::Contracts::ParametersContract::Builder

    Generates an argument 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 argument at the index. 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 argument.

    Parameters:

    • name (String, Symbol)

      The name of the argument.

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

      The expected type of the argument.

    • index (Integer, nil) (defaults to: nil)

      The index of the argument. If not given, then the next argument will be constrained with the type.

    • options (Hash<Symbol, Object>)

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

    Returns:

  • #argument(name, index: nil, **options) {|actual| ... } ⇒ Stannum::Contracts::ParametersContract::Builder

    Generates a new Stannum::Constraint using the block.

    Parameters:

    • name (String, Symbol)

      The name of the argument.

    • index (Integer, nil) (defaults to: nil)

      The index of the argument. If not given, then the next argument will be constrained with the type.

    • options (Hash<Symbol, Object>)

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

    Yields:

    • The definition for the constraint. Each time #matches? is called for this constraint, the given object will be passed to this block and the result of the block will be returned.

    Yield Parameters:

    • actual (Object)

      The object to check against the constraint.

    Yield Returns:

    • (true, false)

      true if the given object matches the constraint, otherwise false.

    Returns:



233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/stannum/contracts/parameters_contract.rb', line 233

def argument(name, type = nil, index: nil, **options, &block)
  type = resolve_constraint_or_type(type, **options, &block)

  contract.add_argument_constraint(
    index,
    type,
    property_name: name,
    **options
  )

  self
end

#arguments(name, type) ⇒ Stannum::Contracts::ParametersContract::Builder

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 the variadic arguments constraint for the contract.

If the parameters includes variadic (or “splatted”) arguments, then each item in the variadic arguments array must match the given type or constraint. If the type is a constraint, then the given constraint will be copied with the given options. If the type is a Class or a Module, then a Stannum::Constraints::Type constraint will be created with the given type.

Parameters:

  • name (String, Symbol)

    a human-readable name for the variadic arguments; used in generating error messages.

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

    The expected type of the variadic arguments items.

Returns:

Raises:

  • (RuntimeError)

    if there is already a variadic arguments constraint defined for the contract.



264
265
266
267
268
# File 'lib/stannum/contracts/parameters_contract.rb', line 264

def arguments(name, type)
  contract.set_arguments_item_constraint(name, type)

  self
end

#block(present) ⇒ Stannum::Contracts::ParametersContract

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 the block parameter constraint for the contract.

If the expected presence is true, a block must be given as part of the parameters. If the expected presence is false, a block must not be given. If the presence is a constraint, then the block must match the constraint.

Parameters:

Returns:

Raises:

  • (RuntimeError)

    if there is already a block constraint defined for the contract.



284
285
286
287
288
# File 'lib/stannum/contracts/parameters_contract.rb', line 284

def block(present)
  contract.set_block_constraint(present)

  self
end

#keyword(name, type, **options) ⇒ Stannum::Contracts::ParametersContract::Builder #keyword(name, **options) {|actual| ... } ⇒ Stannum::Contracts::ParametersContract::Builder

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.

Overloads:

  • #keyword(name, type, **options) ⇒ Stannum::Contracts::ParametersContract::Builder

    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 keyword.

    • options (Hash<Symbol, Object>)

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

    Returns:

  • #keyword(name, **options) {|actual| ... } ⇒ Stannum::Contracts::ParametersContract::Builder

    Generates a new Stannum::Constraint using the block.

    Parameters:

    • keyword (Symbol)

      The keyword to constrain.

    • options (Hash<Symbol, Object>)

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

    Yields:

    • The definition for the constraint. Each time #matches? is called for this constraint, the given object will be passed to this block and the result of the block will be returned.

    Yield Parameters:

    • actual (Object)

      The object to check against the constraint.

    Yield Returns:

    • (true, false)

      true if the given object matches the constraint, otherwise false.

    Returns:



323
324
325
326
327
328
329
330
331
332
333
# File 'lib/stannum/contracts/parameters_contract.rb', line 323

def keyword(name, type = nil, **options, &block)
  type = resolve_constraint_or_type(type, **options, &block)

  contract.add_keyword_constraint(
    name,
    type,
    **options
  )

  self
end

#keywords(name, type) ⇒ Stannum::Contracts::ParametersContract::Builder

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 the variadic keywords constraint for the contract.

If the parameters includes variadic (or “splatted”) keywords, then each value in the variadic keywords hash must match the given type or constraint. If the type is a constraint, then the given constraint will be copied with the given options. If the type is a Class or a Module, then a Stannum::Constraints::Type constraint will be created with the given type.

Parameters:

  • name (String, Symbol)

    a human-readable name for the variadic keywords; used in generating error messages.

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

    The expected type of the variadic keywords values.

Returns:

Raises:

  • (RuntimeError)

    if there is already a variadic keywords constraint defined for the contract.



353
354
355
356
357
# File 'lib/stannum/contracts/parameters_contract.rb', line 353

def keywords(name, type)
  contract.set_keywords_value_constraint(name, type)

  self
end