Class: SafeType::String

Inherits:
Rule
  • Object
show all
Defined in:
lib/safe_type/primitive/string.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rule

#before, coerce, #coerce

Constructor Details

#initialize(type: ::String, min_length: nil, max_length: nil, **args) ⇒ String

Returns a new instance of String.



3
4
5
6
7
# File 'lib/safe_type/primitive/string.rb', line 3

def initialize(type: ::String, min_length: nil, max_length: nil, **args)
  @min_length = min_length
  @max_length = max_length
  super
end

Class Method Details

.default(value = "", min_length: nil, max_length: nil) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/safe_type/primitive/string.rb', line 20

def self.default(value="", min_length: nil, max_length: nil)
  new(
    default: value,
    min_length: min_length,
    max_length: max_length
  )
end

.strict(min_length: nil, max_length: nil) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/safe_type/primitive/string.rb', line 28

def self.strict(min_length: nil, max_length: nil)
  new(
    required: true,
    min_length: min_length,
    max_length: max_length
  )
end

Instance Method Details

#after(input) ⇒ Object



15
16
17
18
# File 'lib/safe_type/primitive/string.rb', line 15

def after(input)
  return nil if input.length == 0
  super
end

#is_valid?(input) ⇒ Boolean

Returns:



9
10
11
12
13
# File 'lib/safe_type/primitive/string.rb', line 9

def is_valid?(input)
  return false unless @min_length.nil? || input.length >= @min_length
  return false unless @max_length.nil? || input.length <= @max_length
  super
end