Class: Waw::Validation::BooleanValidator

Inherits:
Validator show all
Defined in:
lib/waw/validation/boolean_validator.rb

Instance Method Summary collapse

Methods inherited from Validator

#&, #===, #=~, #initialize, #not, #|

Methods included from Helpers

#all_missing?, #any_missing?, #argument_safe, #error, #is_missing?, #missings_to_nil, #no_missing?, #to_validator

Constructor Details

This class inherits a constructor from Waw::Validation::Validator

Instance Method Details

#convert_and_validate(*values) ⇒ Object



25
26
27
28
# File 'lib/waw/validation/boolean_validator.rb', line 25

def convert_and_validate(*values)
  converted = values.collect{|v| to_boolean(v)}
  validate(*converted) ? [true, converted] : [false, values]
end

#is_boolean?(value) ⇒ Boolean

Returns:



5
6
7
# File 'lib/waw/validation/boolean_validator.rb', line 5

def is_boolean?(value)
  (value==true) || (value==false)
end

#to_boolean(value) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/waw/validation/boolean_validator.rb', line 9

def to_boolean(value)
  return value if validate(value)
  case value.to_s.strip
    when 'true'
      true
    when 'false'
      false
    else 
      nil
  end 
end

#validate(*values) ⇒ Object



21
22
23
# File 'lib/waw/validation/boolean_validator.rb', line 21

def validate(*values)
  values.all?{|val| is_boolean?(val)}
end