Class: GreatGuardian::ExpectedValue::String

Inherits:
Base
  • Object
show all
Defined in:
lib/great_guardian/expected_value/string.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#type, #valid_type?

Constructor Details

#initialize(minlen: nil, maxlen: nil, pattern: nil) ⇒ String

Returns a new instance of String.

Raises:

  • (::ArgumentError)

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/great_guardian/expected_value/string.rb', line 14

def initialize(minlen: nil, maxlen: nil, pattern: nil)
  raise ::ArgumentError, minlen.inspect if !minlen.nil? && !minlen.is_a?(::Integer)
  raise ::ArgumentError, maxlen.inspect if !maxlen.nil? && !maxlen.is_a?(::Integer)

  if !minlen.nil? && !maxlen.nil? && (minlen > maxlen)
    raise ::ArgumentError, minlen.inspect, maxlen.inspect
  end

  raise ::ArgumentError, pattern.inspect if !pattern.nil? && !pattern.is_a?(::Regexp)

  super()

  @minlen   = minlen
  @maxlen   = maxlen
  @pattern  = pattern
end

Instance Attribute Details

#maxlenObject (readonly)

Returns the value of attribute maxlen


8
9
10
# File 'lib/great_guardian/expected_value/string.rb', line 8

def maxlen
  @maxlen
end

#minlenObject (readonly)

Returns the value of attribute minlen


8
9
10
# File 'lib/great_guardian/expected_value/string.rb', line 8

def minlen
  @minlen
end

#patternObject (readonly)

Returns the value of attribute pattern


8
9
10
# File 'lib/great_guardian/expected_value/string.rb', line 8

def pattern
  @pattern
end

Class Method Details

.typesObject


10
11
12
# File 'lib/great_guardian/expected_value/string.rb', line 10

def self.types
  [::String]
end

Instance Method Details

#emulate(value) ⇒ Object


40
41
42
# File 'lib/great_guardian/expected_value/string.rb', line 40

def emulate(value)
  value.to_s
end

#first_matched_error_against(actual_value, is_native: true) ⇒ Object


31
32
33
34
35
36
37
38
# File 'lib/great_guardian/expected_value/string.rb', line 31

def first_matched_error_against(actual_value, is_native: true)
  actual_value = emulate(actual_value) unless is_native

  return :type    unless valid_type?(actual_value)
  return :minlen  if !minlen.nil? && actual_value.length < minlen
  return :maxlen  if !maxlen.nil? && actual_value.length > maxlen
  return :pattern if !pattern.nil? && !actual_value.match?(pattern)
end