Class: Crossbeam::Errors

Inherits:
Hash
  • Object
show all
Defined in:
lib/crossbeam/errors.rb

Overview

Used to allow adding errors to the service call similar to ActiveRecord errors

Instance Method Summary collapse

Instance Method Details

#add(key, value, _opts = {}) ⇒ Hash

Add an error to the list of errors

Parameters:

  • key (String, Symbol)

    the key/attribute for the error. (Usually ends up being :base)

  • value (String, Symbol)

    a description of the error

  • _opts (Hash) (defaults to: {})

    additional attributes that get ignored

Returns:

  • (Hash)


17
18
19
20
21
# File 'lib/crossbeam/errors.rb', line 17

def add(key, value, _opts = {})
  self[key] ||= []
  self[key] << value
  self[key].uniq!
end

#add_multiple_errors(errors) ⇒ Hash

Add multiple errors to the error hash

Parameters:

  • errors (Hash<String, Symbol>)

Returns:

  • (Hash)

    , Array]



27
28
29
30
31
32
33
34
35
# File 'lib/crossbeam/errors.rb', line 27

def add_multiple_errors(errors)
  errors.each do |key, values|
    if values.is_a?(String)
      add(key, values)
    elsif [Array, Hash].include?(values.class)
      values.each { |value| add(key, value) }
    end
  end
end

#eachHash, Array

Look through and return a list of all errorr messages

Returns:

  • (Hash, Array)


40
41
42
43
44
# File 'lib/crossbeam/errors.rb', line 40

def each
  each_key do |field|
    self[field].each { |message| yield field, message }
  end
end

#full_messagesArray<String>

Return a full list of error messages

Returns:

  • (Array<String>)


49
50
51
# File 'lib/crossbeam/errors.rb', line 49

def full_messages
  map { |attribute, message| full_message(attribute, message) }.freeze
end

#to_sString

Used to convert the list of errors to a string

Returns:

  • (String)


56
57
58
59
60
# File 'lib/crossbeam/errors.rb', line 56

def to_s
  return '' unless self&.any?

  full_messages.join("\n")
end