class FlexValidations::And
Perform all validations on value to succeed
@example
positive_integer = FlexValidations::And.new \ FlexValidations::Predicate.new(:is_a?, Integer), FlexValidations::Predicate.new(:positive?) [-2, -1.5, -1, 0, 1, 1.5, 2].select(&positive_integer) #=> [1, 2]
@example Description
> puts positive_integer all validations should succeed: - value.is_a?(Integer) should succeed; - value.positive? should succeed.
@example Success result description
> puts positive_integer.validate(1) all validations succeed: - 1.is_a?(Integer) succeed; - 1.positive? succeed.
@example Fail description
> puts positive_integer.validate(-2) -2.positive? failed
Public Class Methods
new(*validations)
click to toggle source
@param validations [Array<FlexValidations::Validation>] all validations that
value should satisfy
@return [FlexValidations::Validation]
# File lib/flex_validations/and.rb, line 34 def initialize(*validations) @validations = validations end
Public Instance Methods
to_s()
click to toggle source
@return [String]
# File lib/flex_validations/and.rb, line 56 def to_s "all validations should succeed:\n" \ "#{IndentedString.new(List.new(@validations))}" end
validate(value)
click to toggle source
@param value [Object] Value to be validated
@return [FlexValidations::Result]
# File lib/flex_validations/and.rb, line 41 def validate(value) successes = [] @validations.each do |validation| res = validation.validate(value) return failed(res, value) if res.fail? successes.push(res) end success(successes, value) end
Private Instance Methods
failed(res, value)
click to toggle source
# File lib/flex_validations/and.rb, line 83 def failed(res, value) Result::Fail::Composite.new(self, res, nil, value, res.raw) end
success(successes, value)
click to toggle source
# File lib/flex_validations/and.rb, line 75 def success(successes, value) Result::Success::Simple.new \ self, SuccessMessage.new(successes), value, value end