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

Returns:

  • (Symbol)

    error key



14
15
16
# File 'lib/diaspora_federation/validators/rules/boolean.rb', line 14

def error_key
  :boolean
end

#paramsHash

This rule has no params.

Returns:

  • (Hash)

    params



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

def params
  {}
end

#valid_value?(value) ⇒ Boolean

Determines if value is a valid boolean

Returns:



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

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

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