Class: Uinit::Type::String
- Defined in:
- lib/uinit/type/string.rb
Instance Attribute Summary collapse
-
#empty ⇒ Object
readonly
Returns the value of attribute empty.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#max_length ⇒ Object
readonly
Returns the value of attribute max_length.
-
#min_length ⇒ Object
readonly
Returns the value of attribute min_length.
Attributes inherited from TypeOf
Instance Method Summary collapse
- #check!(value, depth) ⇒ Object
-
#initialize(empty: true, min_length: nil, max_length: nil, format: nil) ⇒ String
constructor
A new instance of String.
- #inspect ⇒ Object
-
#is?(value) ⇒ Boolean
rubocop:disable Metrics/PerceivedComplexity.
-
#options ⇒ Object
rubocop:enable Metrics/PerceivedComplexity.
Methods inherited from TypeOf
Methods inherited from Base
#==, [], from, from?, #is!, #name, #to_s, #trace!, #type_error!
Methods included from Operators
Constructor Details
#initialize(empty: true, min_length: nil, max_length: nil, format: nil) ⇒ String
6 7 8 9 10 11 12 13 |
# File 'lib/uinit/type/string.rb', line 6 def initialize(empty: true, min_length: nil, max_length: nil, format: nil) super(::String) @empty = empty @min_length = min_length @max_length = max_length @format = format end |
Instance Attribute Details
#empty ⇒ Object (readonly)
Returns the value of attribute empty.
15 16 17 |
# File 'lib/uinit/type/string.rb', line 15 def empty @empty end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
15 16 17 |
# File 'lib/uinit/type/string.rb', line 15 def format @format end |
#max_length ⇒ Object (readonly)
Returns the value of attribute max_length.
15 16 17 |
# File 'lib/uinit/type/string.rb', line 15 def max_length @max_length end |
#min_length ⇒ Object (readonly)
Returns the value of attribute min_length.
15 16 17 |
# File 'lib/uinit/type/string.rb', line 15 def min_length @min_length end |
Instance Method Details
#check!(value, depth) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/uinit/type/string.rb', line 29 def check!(value, depth) super if !empty && (value.empty? || value.match?(/^\s+$/)) type_error!('String cannot be empty or contain only whitespace', depth) end if min_length && value.length < min_length type_error!("String too short (minimum #{min_length} characters)", depth) end if max_length && value.length > max_length type_error!("String too long (maximum #{max_length} characters)", depth) end type_error!('String doesn\'t match required pattern', depth) if format && !value.match?(format) value end |
#inspect ⇒ Object
54 55 56 |
# File 'lib/uinit/type/string.rb', line 54 def inspect "$#{name}[#{options.inspect}]" end |
#is?(value) ⇒ Boolean
rubocop:disable Metrics/PerceivedComplexity
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/uinit/type/string.rb', line 18 def is?(value) return false unless super return false if !empty && (value.empty? || value.match?(/^\s+$/)) return false if min_length && value.length < min_length return false if max_length && value.length > max_length return false if format && !value.match?(format) true end |
#options ⇒ Object
rubocop:enable Metrics/PerceivedComplexity
50 51 52 |
# File 'lib/uinit/type/string.rb', line 50 def { empty:, min_length:, max_length:, format: }.compact end |