Class: Burner::Library::Collection::Validate

Inherits:
JobWithRegister show all
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

JobWithRegister::BLANK

Instance Attribute Summary collapse

Attributes inherited from JobWithRegister

#register

Attributes inherited from Job

#name

Instance Method Summary collapse

Methods included from Util::Arrayable

#array

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      = message_key.to_s
  @resolver         = Objectable.resolver(separator: separator)
  @validations      = Modeling::Validations.array(validations)

  freeze
end

Instance Attribute Details

#invalid_registerObject (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_charObject (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_keyObject (readonly)

Returns the value of attribute message_key.



25
26
27
# File 'lib/burner/library/collection/validate.rb', line 25

def message_key
  @message_key
end

#resolverObject (readonly)

Returns the value of attribute resolver.



25
26
27
# File 'lib/burner/library/collection/validate.rb', line 25

def resolver
  @resolver
end

#validationsObject (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