Class: Waw::Validation::OrValidator

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

Instance Method Summary collapse

Methods inherited from Validator

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

Methods included from Helpers

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

Constructor Details

#initialize(*validators) ⇒ OrValidator

Empty constructor that overrides the top one



6
7
8
# File 'lib/waw/validation/or_validator.rb', line 6

def initialize(*validators)
  @validators = validators
end

Instance Method Details

#convert_and_validate(*values) ⇒ Object

Converts and validate



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/waw/validation/or_validator.rb', line 16

def convert_and_validate(*values)
  converted = []
  values.each do |value|
    found = false
    @validators.each do |validator|
      found, val_converted = validator.convert_and_validate(value)
      if found
        converted << val_converted[0]
        break
      end
    end
    return [false, values] unless found
  end
  [true, converted]
end

#validate(*values) ⇒ Object

Calls the block installed at initialization time



11
12
13
# File 'lib/waw/validation/or_validator.rb', line 11

def validate(*values)
  values.all?{|val| @validators.any?{|validator| validator.validate(val)}}
end