Class: Ward::ValidatorSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ward/validator_set.rb

Overview

Holds one or more Validators which are associated with a particular class.

This class is considered part of Ward’s semi-public API; which is to say that it’s methods should not be called in end-user applications, but are available for library and plugin authors who wish to extend it’s functionality.

Examples:

Retrieving the validators for a class.

MyClass.validators # => #<ValidatorSet [#<Validator>, ...]>

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(validators = []) ⇒ ValidatorSet

Creates a new ValidatorSet.

Parameters:

  • validators (Enumerable<Ward::Validator>) (defaults to: [])

    An optional collection of validators.



35
36
37
# File 'lib/ward/validator_set.rb', line 35

def initialize(validators = [])
  @validators = validators
end

Class Method Details

.build(set = nil, &block) ⇒ Ward::ValidatorSet

Builds a ValidatorSet using the given block.

NOTE: Providing an existing ValidatorSet will result in a copy of that set being mutated; the original will not be changed.

Parameters:

  • set (Ward::ValidatorSet) (defaults to: nil)

    A ValidatorSet to which the built validators should be added.

Returns:



24
25
26
# File 'lib/ward/validator_set.rb', line 24

def self.build(set = nil, &block)
  Ward::DSL::ValidationBlock.new(set, &block).to_validator_set
end

Instance Method Details

#each {|validator| ... } ⇒ Object

Iterates through each validator in the set.

Yields:

  • (validator)

    Yields each validator in turn.

Yield Parameters:



88
89
90
# File 'lib/ward/validator_set.rb', line 88

def each(&block)
  @validators.each(&block)
end

#merge!(other) ⇒ Object

Adds the validators contained in other to the receiving set.

Parameters:

  • set (Ward::ValidatorSet)

    The validator set whose validators are to be added to the receiver.



109
110
111
112
# File 'lib/ward/validator_set.rb', line 109

def merge!(other)
  @validators |= other.to_a
  self
end

#push(validator) ⇒ Object Also known as: <<

Adds a new validator to set.

Parameters:



97
98
99
# File 'lib/ward/validator_set.rb', line 97

def push(validator)
  @validators << validator
end

#valid?(record, scenario = :default) ⇒ Boolean

Determines if all of the contained validators are valid for the given record in the given scenario.

Parameters:

  • record (Object)

    The object whose validations are to be run.

  • scenario (Symbol) (defaults to: :default)

    A name identifying the scenario.

Returns:

  • (Boolean)

    Returns true if the record validated, otherwise returns false.



50
51
52
53
54
55
# File 'lib/ward/validator_set.rb', line 50

def valid?(record, scenario = :default)
  inject(true) do |result, validator|
    (! validator.scenario?(scenario) or
      validator.validate(record).first) and result
  end
end

#validate(record, scenario = :default) ⇒ Ward::Support::Result

A more useful version of #valid?

Whereas #valid? simply returns true or false indicating whether the validations passed, #validate returns a Ward::Support::Result instance which describes the outcome of running the validators, and encapsulates any errors messages which resulted.

Parameters:

  • record (Object)

    The object whose validations are to be run.

  • scenario (Symbol) (defaults to: :default)

    A name identifying the scenario.

Returns:



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ward/validator_set.rb', line 71

def validate(record, scenario = :default)
  errors = Ward::Errors.new

  @validators.each do |validator|
    next unless validator.scenario?(scenario)
    result, error = validator.validate(record)
    errors.add(validator.context, error) unless result == true
  end

  Support::Result.new(errors)
end