module Stannum::ParameterValidation::ClassMethods

Defines a DSL for validating method parameters.

@see ParameterValidation.

Public Instance Methods

validate_parameters(method_name, &validations) click to toggle source

Creates a validation contract and wraps the named method.

The provided block is used to create a ParametersContract, and supports the same DSL used to define one.

@see Stannum::Contracts::ParametersContract

Calls superclass method
# File lib/stannum/parameter_validation.rb, line 120
def validate_parameters(method_name, &validations)
  method_name = method_name.intern
  contract    = Stannum::Contracts::ParametersContract.new(&validations)

  self::MethodValidations.add_contract(method_name, contract)

  self::MethodValidations.define_method(method_name) \
  do |*arguments, **keywords, &block|
    result = match_parameters_to_contract(
      arguments:   arguments,
      block:       block,
      contract:    contract,
      keywords:    keywords,
      method_name: method_name
    )

    return result unless result == VALIDATION_SUCCESS

    if keywords.empty?
      super(*arguments, &block)
    else
      super(*arguments, **keywords, &block)
    end
  end
end

Private Instance Methods

inherited(subclass) click to toggle source

rubocop:enable Metrics/MethodLength

Calls superclass method
# File lib/stannum/parameter_validation.rb, line 149
def inherited(subclass)
  super

  Stannum::ParameterValidation.add_method_validations(subclass)

  subclass::MethodValidations.include(self::MethodValidations)
end