class FlexValidations::All

Check if all elements in enumerable are valid

@example

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

Public Class Methods

new(validation) click to toggle source

@param validation [FlexValidations::Validation] validation which performed on each element

# File lib/flex_validations/all.rb, line 11
def initialize(validation)
  @validation = validation
end

Public Instance Methods

to_s() click to toggle source

@return [String]

# File lib/flex_validations/all.rb, line 31
def to_s
  "all elements should pass following validation:\n" \
    "#{IndentedString.new(@validation)}"
end
validate(value) click to toggle source

@param value [#each] Value to be validated

@return [FlexValidations::Result]

# 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

Private Instance Methods

failed(value, res, element, index) click to toggle source
# File lib/flex_validations/all.rb, line 94
def failed(value, res, element, index)
  Result::Fail::Composite.new \
    self,
    res,
    FailedMessage.new(res, element, index),
    value,
    res.raw
end
not_enumerable(value) click to toggle source
# File lib/flex_validations/all.rb, line 86
def not_enumerable(value)
  Result::Fail::Simple.new \
    self,
    NotEnumerableMessage.new(value),
    value,
    false
end
success(value) click to toggle source
# File lib/flex_validations/all.rb, line 78
def success(value)
  Result::Success::Simple.new \
    self,
    SuccessMessage.new(@validation),
    value,
    value
end