Class: Ward::ValidatorSet
- Inherits:
-
Object
- Object
- Ward::ValidatorSet
- 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.
Class Method Summary collapse
-
.build(set = nil, &block) ⇒ Ward::ValidatorSet
Builds a ValidatorSet using the given block.
Instance Method Summary collapse
-
#each {|validator| ... } ⇒ Object
Iterates through each validator in the set.
-
#initialize(validators = []) ⇒ ValidatorSet
constructor
Creates a new ValidatorSet.
-
#merge!(other) ⇒ Object
Adds the validators contained in
other
to the receiving set. -
#push(validator) ⇒ Object
(also: #<<)
Adds a new validator to set.
-
#valid?(record, scenario = :default) ⇒ Boolean
Determines if all of the contained validators are valid for the given record in the given scenario.
-
#validate(record, scenario = :default) ⇒ Ward::Support::Result
A more useful version of #valid?.
Constructor Details
#initialize(validators = []) ⇒ ValidatorSet
Creates a new ValidatorSet.
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.
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.
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.
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.
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.
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.
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 |