Class: Highway::Steps::Types::Any

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

Overview

This class represents any parameter type. It can be used in parameters which should not perform any type checking.

Direct Known Subclasses

AnyOf, Array, Bool, Hash, Number, String

Instance Method Summary collapse

Constructor Details

#initialize(validate: nil) ⇒ Any

Initialize an instance.

Parameters:

  • validate (Proc) (defaults to: nil)

    A custom value validation block.



19
20
21
# File 'lib/highway/steps/types/any.rb', line 19

def initialize(validate: nil)
  @validate = validate
end

Instance Method Details

#typecheck(value) ⇒ Object?

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:

  • (Object, nil)


31
32
33
# File 'lib/highway/steps/types/any.rb', line 31

def typecheck(value)
  value
end

#typecheck_and_validate(value) ⇒ Object?

Typecheck and validate the value at the same time.

This method returns typechecked, coerced and validated value or ‘nil` if value has invalid type, can’t be coerced or is invalid.

Parameters:

  • value (Object)

    A value.

Returns:

  • (Object, nil)


56
57
58
59
# File 'lib/highway/steps/types/any.rb', line 56

def typecheck_and_validate(value)
  typechecked = typecheck(value)
  typechecked if !typechecked.nil? && validate(typechecked)
end

#validate(value) ⇒ Boolean

Validate the typechecked value against a custom validation block.

This method returns ‘true` if value is valid or `false` if value is invalid.

Parameters:

  • value (Object)

    A value.

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/highway/steps/types/any.rb', line 43

def validate(value)
  return true if @validate == nil
  @validate.call(value)
end