Class: Ronin::Core::Params::Types::String
- Defined in:
- lib/ronin/core/params/types/string.rb
Overview
Represents a string type.
Instance Attribute Summary collapse
-
#format ⇒ ::Regexp?
readonly
Optional regexp to validate values with.
Instance Method Summary collapse
-
#allow_blank? ⇒ ::Boolean
private
Specifies whether the param may accept blank values.
-
#allow_empty? ⇒ ::Boolean
private
Specifies whether the param may accept empty values.
-
#coerce(value) ⇒ ::String
private
Validates the given value.
-
#initialize(allow_empty: false, allow_blank: false, format: nil) ⇒ String
constructor
Initializes the value.
Constructor Details
#initialize(allow_empty: false, allow_blank: false, format: nil) ⇒ String
Initializes the 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.
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.
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.
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.
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 |