class Highway::Steps::Types::Array

This class represents an array parameter type.

Public Class Methods

new(element_type, validate: nil) click to toggle source

Initialize an instance.

@param element_type [Object] Type of inner elements. @param validate [Proc] A custom value validation block.

Calls superclass method Highway::Steps::Types::Any::new
# File lib/highway/steps/types/array.rb, line 21
def initialize(element_type, validate: nil)
  super(validate: validate)
  @element_type = element_type
end

Public Instance Methods

typecheck(value) click to toggle source

Typecheck and coerce a value if possible.

This method returns a typechecked and coerced value or `nil` if value has invalid type and can't be coerced.

@param value [Object] A value.

@return [Array, nil]

# File lib/highway/steps/types/array.rb, line 34
def typecheck(value)
  return nil unless value.kind_of?(::Array)
  typechecked = value.map { |element| @element_type.typecheck_and_validate(element) }
  typechecked if typechecked.all? { |element| !element.nil? }
end