Class: Ronin::Core::Params::Types::String

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

Overview

Represents a string type.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(allow_empty: false, allow_blank: false, format: nil) ⇒ String

Initializes the value.

Parameters:

  • allow_empty (::Boolean) (defaults to: false)

    Specifies whether the argument may accept empty values.

  • allow_blank (::Boolean) (defaults to: false)

    Specifies whether the argument may accept blank values.

  • format ((::Regexp, ::String), nil) (defaults to: nil)

    Optional regular expression to validate the given param value.



47
48
49
50
51
52
# File 'lib/ronin/core/params/types/string.rb', line 47

def initialize(allow_empty: false, allow_blank: false, format: nil)
  @allow_empty = allow_empty
  @allow_blank = allow_blank

  @format = format
end

Instance Attribute Details

#format::Regexp? (readonly)

Optional regexp to validate values with.

Returns:

  • (::Regexp, nil)


33
34
35
# File 'lib/ronin/core/params/types/string.rb', line 33

def format
  @format
end

Instance Method Details

#allow_blank?::Boolean

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.

Specifies whether the param may accept blank values.

Returns:

  • (::Boolean)


72
73
74
# File 'lib/ronin/core/params/types/string.rb', line 72

def allow_blank?
  @allow_blank
end

#allow_empty?::Boolean

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.

Specifies whether the param may accept empty values.

Returns:

  • (::Boolean)


61
62
63
# File 'lib/ronin/core/params/types/string.rb', line 61

def allow_empty?
  @allow_empty
end

#coerce(value) ⇒ ::String

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.

Validates the given value.

Parameters:

  • value (Object)

    The given value to validate.

Returns:

  • (::String)

    The coerced String.

Raises:

  • (ValidationError)

    The given value was not a String and did not define a #to_s method, or was a String that did not match #format.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ronin/core/params/types/string.rb', line 91

def coerce(value)
  case value
  when Enumerable
    raise(ValidationError,"cannot convert an Enumerable into a String (#{value.inspect})")
  else
    unless value.respond_to?(:to_s)
      raise(ValidationError,"value does not define a #to_s method (#{value.inspect})")
    end

    string = value.to_s

    if @format && string !~ @format
      raise(ValidationError,"does not match the format (#{string.inspect})")
    elsif (string.empty? && !allow_empty?)
      raise(ValidationError,"value cannot be empty")
    elsif (string =~ /\A\s+\z/ && !allow_blank?)
      raise(ValidationError,"value cannot contain all whitespace (#{string.inspect})")
    end

    return string
  end
end