Class: Domainic::Command::Result::ErrorSet
- Inherits:
-
Object
- Object
- Domainic::Command::Result::ErrorSet
- Defined in:
- lib/domainic/command/result/error_set.rb
Overview
A flexible container for managing and formatting command errors. The ErrorSet provides a consistent
interface for working with errors from various sources including simple strings, arrays, hashes,
standard errors, and objects implementing a compatible to_h interface (like ActiveModel::Errors).
Instance Method Summary collapse
-
#[](key) ⇒ Array<String>?
Retrieves error messages for a specific key.
-
#add(key, message) ⇒ void
Adds a new error message for a specific key.
-
#clear ⇒ void
Clear all errors from the set.
-
#empty? ⇒ Boolean
Check if the error set is empty.
-
#full_messages ⇒ Array<String>
(also: #to_a, #to_array, #to_ary)
Returns all error messages with their keys.
-
#initialize(errors = nil) ⇒ ErrorSet
constructor
Creates a new ErrorSet instance.
-
#messages ⇒ Hash{Symbol => Array<String>}
(also: #to_h, #to_hash)
Returns a hash of all error messages.
Constructor Details
#initialize(errors = nil) ⇒ ErrorSet
Creates a new ErrorSet instance
41 42 43 |
# File 'lib/domainic/command/result/error_set.rb', line 41 def initialize(errors = nil) @lookup = Parser.new(errors).parse! end |
Instance Method Details
#[](key) ⇒ Array<String>?
Retrieves error messages for a specific key
50 51 52 |
# File 'lib/domainic/command/result/error_set.rb', line 50 def [](key) @lookup[key.to_sym] end |
#add(key, message) ⇒ void
This method returns an undefined value.
Adds a new error message for a specific key
61 62 63 64 65 |
# File 'lib/domainic/command/result/error_set.rb', line 61 def add(key, ) key = key.to_sym @lookup[key] ||= [] @lookup[key].concat(Array()) # steep:ignore ArgumentTypeMismatch end |
#clear ⇒ void
This method returns an undefined value.
Clear all errors from the set
71 72 73 |
# File 'lib/domainic/command/result/error_set.rb', line 71 def clear @lookup = {} end |
#empty? ⇒ Boolean
Check if the error set is empty
79 80 81 |
# File 'lib/domainic/command/result/error_set.rb', line 79 def empty? @lookup.empty? end |
#full_messages ⇒ Array<String> Also known as: to_a, to_array, to_ary
Returns all error messages with their keys
87 88 89 90 91 |
# File 'lib/domainic/command/result/error_set.rb', line 87 def @lookup.each_with_object([]) do |(key, ), result| result.concat(.map { || "#{key} #{message}" }) end end |
#messages ⇒ Hash{Symbol => Array<String>} Also known as: to_h, to_hash
Returns a hash of all error messages
100 101 102 |
# File 'lib/domainic/command/result/error_set.rb', line 100 def @lookup.dup.freeze end |