Class: Crossbeam::Errors
- Inherits:
-
Hash
- Object
- Hash
- Crossbeam::Errors
- Defined in:
- lib/crossbeam/errors.rb
Overview
Used to allow adding errors to the service call similar to ActiveRecord errors
Instance Method Summary collapse
-
#add(key, value, _opts = {}) ⇒ Hash
Add an error to the list of errors.
-
#add_multiple_errors(errors) ⇒ Hash
Add multiple errors to the error hash.
-
#each ⇒ Hash, Array
Look through and return a list of all errorr messages.
-
#full_messages ⇒ Array<String>
Return a full list of error messages.
-
#to_s ⇒ String
Used to convert the list of errors to a string.
Instance Method Details
#add(key, value, _opts = {}) ⇒ Hash
Add an error to the list of errors
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
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 |
#each ⇒ Hash, Array
Look through and return a list of all errorr messages
40 41 42 43 44 |
# File 'lib/crossbeam/errors.rb', line 40 def each each_key do |field| self[field].each { || yield field, } end end |
#full_messages ⇒ Array<String>
Return a full list of error messages
49 50 51 |
# File 'lib/crossbeam/errors.rb', line 49 def map { |attribute, | (attribute, ) }.freeze end |
#to_s ⇒ String
Used to convert the list of errors to a string
56 57 58 59 60 |
# File 'lib/crossbeam/errors.rb', line 56 def to_s return '' unless self&.any? .join("\n") end |