Class: Validation::Rule::Boolean

Inherits:
Object
  • Object
show all
Defined in:
lib/diaspora_federation/validators/rules/boolean.rb

Overview

Boolean validation rule

Valid is:

  • a String: “true”, “false”, “t”, “f”, “yes”, “no”, “y”, “n”, “1”, “0”

  • a Integer: 1 or 0

  • a Boolean: true or false

Instance Method Summary collapse

Instance Method Details

#error_keySymbol

The error key for this rule


12
13
14
# File 'lib/diaspora_federation/validators/rules/boolean.rb', line 12

def error_key
  :boolean
end

#paramsHash

This rule has no params.


33
34
35
# File 'lib/diaspora_federation/validators/rules/boolean.rb', line 33

def params
  {}
end

#valid_value?(value) ⇒ Boolean

Determines if value is a valid boolean


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/diaspora_federation/validators/rules/boolean.rb', line 17

def valid_value?(value)
  return false if value.nil?

  if value.is_a?(String)
    true if value =~ /\A(true|false|t|f|yes|no|y|n|1|0)\z/i
  elsif value.is_a?(Integer)
    true if [1, 0].include?(value)
  elsif [true, false].include?(value)
    true
  else
    false
  end
end