class Highway::Steps::Types::String

This class represents a string parameter type.

Public Class Methods

regex(regex) click to toggle source

Initialize an instance.

@param regex [Regexp] A regular expression string must match.

@return [Highway::Steps::Types::String]

# File lib/highway/steps/types/string.rb, line 22
def self.regex(regex)
  self.new(validate: lambda { |value| regex =~ value })
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 [String, nil]

# 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