class Highway::Steps::Types::Hash

This class represents a dictionary 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/hash.rb, line 22
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 [Hash, nil]

# File lib/highway/steps/types/hash.rb, line 35
def typecheck(value)
  return nil unless value.is_a?(::Hash)
  typechecked = Utilities::hash_map(value) { |key, element| [key, @element_type.typecheck_and_validate(element)] }
  typechecked if typechecked.values.all? { |element| !element.nil? }
end