Class: FlexValidations::And

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

Overview

Perform all validations on value to succeed

Examples:

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]

Description

> puts positive_integer
all validations should succeed:
  - value.is_a?(Integer) should succeed;
  - value.positive? should succeed.

Success result description

> puts positive_integer.validate(1)
all validations succeed:
  - 1.is_a?(Integer) succeed;
  - 1.positive? succeed.

Fail description

> puts positive_integer.validate(-2)
-2.positive? failed

Defined Under Namespace

Classes: SuccessMessage

Instance Method Summary collapse

Methods included from Validation

#===, #to_proc

Constructor Details

#initialize(*validations) ⇒ FlexValidations::Validation

Parameters:



34
35
36
# File 'lib/flex_validations/and.rb', line 34

def initialize(*validations)
  @validations = validations
end

Instance Method Details

#to_sString

Returns:

  • (String)


56
57
58
59
# File 'lib/flex_validations/and.rb', line 56

def to_s
  "all validations should succeed:\n" \
    "#{IndentedString.new(List.new(@validations))}"
end

#validate(value) ⇒ FlexValidations::Result

Parameters:

  • value (Object)

    Value to be validated

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
# 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