Class: Highway::Steps::Types::Enum

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

Overview

This class represents an enum parameter type. It can be used in parameters which have a finite set of valid values.

Instance Method Summary collapse

Methods inherited from String

regex

Methods inherited from Any

#typecheck_and_validate, #validate

Constructor Details

#initialize(*values) ⇒ Enum

Initialize an instance.

Parameters:

  • *values (String)

    Allowed enum values.



22
23
24
25
# File 'lib/highway/steps/types/enum.rb', line 22

def initialize(*values)
  super(validate: nil)
  @values = values
end

Instance Method Details

#typecheck(value) ⇒ String?

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:



35
36
37
38
# File 'lib/highway/steps/types/enum.rb', line 35

def typecheck(value)
  typechecked = super(value)
  typechecked if !typechecked.nil? && @values.include?(typechecked)
end