class Burner::Library::Collection::Validate
Process each object in an array and see if its attribute values match a given set of validations. The main register will include the valid objects and the invalid_register
will contain the invalid objects.
Expected Payload input: array of objects. Payload output: An array of objects that are valid. Payload output: An array of objects that are invalid.
Constants
- DEFAULT_INVALID_REGISTER
- DEFAULT_JOIN_CHAR
- DEFAULT_MESSAGE_KEY
Attributes
invalid_register[R]
join_char[R]
message_key[R]
resolver[R]
validations[R]
Public Class Methods
new( invalid_register: DEFAULT_INVALID_REGISTER, join_char: DEFAULT_JOIN_CHAR, message_key: DEFAULT_MESSAGE_KEY, name: '', register: DEFAULT_REGISTER, separator: '', validations: [] )
click to toggle source
Calls superclass method
Burner::JobWithRegister::new
# File lib/burner/library/collection/validate.rb, line 31 def initialize( invalid_register: DEFAULT_INVALID_REGISTER, join_char: DEFAULT_JOIN_CHAR, message_key: DEFAULT_MESSAGE_KEY, name: '', register: DEFAULT_REGISTER, separator: '', validations: [] ) super(name: name, register: register) @invalid_register = invalid_register.to_s @join_char = join_char.to_s @message_key = message_key.to_s @resolver = Objectable.resolver(separator: separator) @validations = Modeling::Validations.array(validations) freeze end
Public Instance Methods
perform(output, payload)
click to toggle source
# File lib/burner/library/collection/validate.rb, line 51 def perform(output, payload) valid = [] invalid = [] (payload[register] || []).each do |object| errors = validate(object) if errors.empty? valid << object else invalid << make_in_error(object, errors) end end output.detail("Valid count: #{valid.length}") output.detail("Invalid count: #{invalid.length}") payload[register] = valid payload[invalid_register] = invalid nil end
Private Instance Methods
make_in_error(object, errors)
click to toggle source
# File lib/burner/library/collection/validate.rb, line 84 def make_in_error(object, errors) resolver.set(object, message_key, errors.join(join_char)) end
validate(object)
click to toggle source
# File lib/burner/library/collection/validate.rb, line 76 def validate(object) validations.each_with_object([]) do |validation, memo| next if validation.valid?(object, resolver) memo << validation.message end end