Class: MiniDefender::Rules::String

Inherits:
MiniDefender::Rule show all
Defined in:
lib/mini_defender/rules/string.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MiniDefender::Rule

#active?, available?, #bails?, #default_value, #defaults?, #error_message, #excluded?, #force_coerce?, #implicit?, #priority, #stops?, #with_message

Constructor Details

#initialize(mode = 'strict') ⇒ String

Returns a new instance of String.



4
5
6
# File 'lib/mini_defender/rules/string.rb', line 4

def initialize(mode = 'strict')
  @mode = mode
end

Class Method Details

.make(args) ⇒ Object



12
13
14
# File 'lib/mini_defender/rules/string.rb', line 12

def self.make(args)
  new(args[0] || 'strict')
end

.signatureObject



8
9
10
# File 'lib/mini_defender/rules/string.rb', line 8

def self.signature
  'string'
end

Instance Method Details

#coerce(value) ⇒ Object



16
17
18
# File 'lib/mini_defender/rules/string.rb', line 16

def coerce(value)
  value.to_s
end

#message(attribute, value, validator) ⇒ Object



32
33
34
# File 'lib/mini_defender/rules/string.rb', line 32

def message(attribute, value, validator)
  "The value must be a string."
end

#passes?(attribute, value, validator) ⇒ Boolean

Returns:



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mini_defender/rules/string.rb', line 20

def passes?(attribute, value, validator)
  if @mode == 'relaxed'
    value.is_a?(String) ||
      value.is_a?(Integer) ||
      value.is_a?(Float) ||
      value.is_a?(FalseClass) ||
      value.is_a?(TrueClass)
  else
    value.is_a?(String)
  end
end