Class: FlexValidations::All

Inherits:
Object
  • Object
show all
Defined in:
lib/flex_validations/all.rb

Overview

Check if all elements in enumerable are valid

Examples:

all_odd = FlexValidations::All.new(odd)
all_odd.validate([1, 2, 3]).success? # => false

Defined Under Namespace

Classes: FailedMessage, NotEnumerableMessage, SuccessMessage

Instance Method Summary collapse

Constructor Details

#initialize(validation) ⇒ All

Returns a new instance of All.

Parameters:



11
12
13
# File 'lib/flex_validations/all.rb', line 11

def initialize(validation)
  @validation = validation
end

Instance Method Details

#to_sString

Returns:

  • (String)


31
32
33
34
# File 'lib/flex_validations/all.rb', line 31

def to_s
  "all elements should pass following validation:\n" \
    "#{IndentedString.new(@validation)}"
end

#validate(value) ⇒ FlexValidations::Result

Parameters:

  • value (#each)

    Value to be validated

Returns:



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/flex_validations/all.rb', line 18

def validate(value)
  return not_enumerable(value) unless value.respond_to?(:each, false)

  value.each.with_index do |element, index|
    res = @validation.validate(element)

    return failed(value, res, element, index) if res.fail?
  end

  success(value)
end