Class: MiniDefender::Rules::Boolean

Inherits:
MiniDefender::Rule show all
Defined in:
lib/mini_defender/rules/boolean.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?, make, #priority, #stops?, #with_message

Class Method Details

.signatureObject



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

def self.signature
  'boolean'
end

Instance Method Details

#coerce(value) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/mini_defender/rules/boolean.rb', line 8

def coerce(value)
  value = value.downcase if value.is_a?(String)
  case value
  when 0, '0', 'no', 'off', 'false'
    false
  when 1, '1', 'yes', 'on', 'true'
    true
  else
    value
  end
end

#message(attribute, value, validator) ⇒ Object



26
27
28
# File 'lib/mini_defender/rules/boolean.rb', line 26

def message(attribute, value, validator)
  'The value must be a boolean, yes, no, on, off, 0 or 1.'
end

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

Returns:



20
21
22
23
24
# File 'lib/mini_defender/rules/boolean.rb', line 20

def passes?(attribute, value, validator)
  value = value.downcase if value.is_a?(String)
  value.is_a?(TrueClass) || value.is_a?(FalseClass) ||
    [1, 0, '1', '0', 'yes', 'no', 'on', 'off', 'true', 'false'].include?(value)
end