Class: Ronin::Core::Params::Types::Regexp

Inherits:
Type
  • Object
show all
Defined in:
lib/ronin/core/params/types/regexp.rb

Overview

Represents a Regexp type.

Instance Method Summary collapse

Instance Method Details

#coerce(value) ⇒ Regexp

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parses the String value.

Parameters:

  • value (::String, ::Regexp, Object)

    The String value to coerce.

Returns:

  • (Regexp)

    The coerced Regexp value.

Raises:

  • (ValidationError)

    The value was not a Regexp, a String, or a String that could not be parsed by Regexp.new.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ronin/core/params/types/regexp.rb', line 45

def coerce(value)
  case value
  when ::Regexp then value
  when ::String
    unless (value.start_with?('/') && value.end_with?('/'))
      raise(ValidationError,"value must be of the format '/.../' (#{value.inspect})")
    end

    begin
      ::Regexp.new(value[1..-2])
    rescue RegexpError
      raise(ValidationError,"value is not a valid Regexp (#{value.inspect})")
    end
  else
    raise(ValidationError,"value must be either a String or a Regexp (#{value.inspect})")
  end
end