Class: Highway::Steps::Types::String

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

Overview

This class represents a string parameter type.

Direct Known Subclasses

Enum, Url

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Any

#initialize, #typecheck_and_validate, #validate

Constructor Details

This class inherits a constructor from Highway::Steps::Types::Any

Class Method Details

.regex(regex) ⇒ Highway::Steps::Types::String

Initialize an instance.

Parameters:

  • regex (Regexp)

    A regular expression string must match.

Returns:

[View source]

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

def self.regex(regex)
  self.new(validate: lambda { |value| regex =~ value })
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:

[View source]

34
35
36
37
38
39
40
41
# File 'lib/highway/steps/types/string.rb', line 34

def typecheck(value)
  case value
    when ::String then value
    when ::Numeric then value.to_s
    when ::TrueClass then "true"
    when ::FalseClass then "false"
  end
end