Class: Burner::Library::Collection::Validate
- Inherits:
-
JobWithRegister
- Object
- Job
- JobWithRegister
- Burner::Library::Collection::Validate
- Defined in:
- lib/burner/library/collection/validate.rb
Overview
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.
Constant Summary collapse
- DEFAULT_INVALID_REGISTER =
'invalid'
- DEFAULT_JOIN_CHAR =
', '
- DEFAULT_MESSAGE_KEY =
'errors'
Constants inherited from JobWithRegister
Instance Attribute Summary collapse
-
#invalid_register ⇒ Object
readonly
Returns the value of attribute invalid_register.
-
#join_char ⇒ Object
readonly
Returns the value of attribute join_char.
-
#message_key ⇒ Object
readonly
Returns the value of attribute message_key.
-
#resolver ⇒ Object
readonly
Returns the value of attribute resolver.
-
#validations ⇒ Object
readonly
Returns the value of attribute validations.
Attributes inherited from JobWithRegister
Attributes inherited from Job
Instance Method Summary collapse
-
#initialize(invalid_register: DEFAULT_INVALID_REGISTER, join_char: DEFAULT_JOIN_CHAR, message_key: DEFAULT_MESSAGE_KEY, name: '', register: DEFAULT_REGISTER, separator: '', validations: []) ⇒ Validate
constructor
A new instance of Validate.
- #perform(output, payload) ⇒ Object
Methods included from Util::Arrayable
Constructor Details
#initialize(invalid_register: DEFAULT_INVALID_REGISTER, join_char: DEFAULT_JOIN_CHAR, message_key: DEFAULT_MESSAGE_KEY, name: '', register: DEFAULT_REGISTER, separator: '', validations: []) ⇒ Validate
Returns a new instance of Validate.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# 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 = .to_s @resolver = Objectable.resolver(separator: separator) @validations = Modeling::Validations.array(validations) freeze end |
Instance Attribute Details
#invalid_register ⇒ Object (readonly)
Returns the value of attribute invalid_register.
25 26 27 |
# File 'lib/burner/library/collection/validate.rb', line 25 def invalid_register @invalid_register end |
#join_char ⇒ Object (readonly)
Returns the value of attribute join_char.
25 26 27 |
# File 'lib/burner/library/collection/validate.rb', line 25 def join_char @join_char end |
#message_key ⇒ Object (readonly)
Returns the value of attribute message_key.
25 26 27 |
# File 'lib/burner/library/collection/validate.rb', line 25 def @message_key end |
#resolver ⇒ Object (readonly)
Returns the value of attribute resolver.
25 26 27 |
# File 'lib/burner/library/collection/validate.rb', line 25 def resolver @resolver end |
#validations ⇒ Object (readonly)
Returns the value of attribute validations.
25 26 27 |
# File 'lib/burner/library/collection/validate.rb', line 25 def validations @validations end |
Instance Method Details
#perform(output, payload) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# 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 |