Class: Highway::Steps::Types::Array

Inherits:
Any
  • Object
show all
Defined in:
lib/highway/steps/types/array.rb

Overview

This class represents an array parameter type.

Direct Known Subclasses

Set

Instance Method Summary collapse

Methods inherited from Any

#typecheck_and_validate, #validate

Constructor Details

#initialize(element_type, validate: nil) ⇒ Array

Initialize an instance.

Parameters:

  • element_type (Object)

    Type of inner elements.

  • validate (Proc) (defaults to: nil)

    A custom value validation block.



21
22
23
24
# File 'lib/highway/steps/types/array.rb', line 21

def initialize(element_type, validate: nil)
  super(validate: validate)
  @element_type = element_type
end

Instance Method Details

#typecheck(value) ⇒ Array?

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.

Parameters:

  • value (Object)

    A value.

Returns:



34
35
36
37
38
# 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