Class: SimpleCommandDispatcher::Commands::CommandCallable::Errors
- Inherits:
-
Hash
- Object
- Hash
- SimpleCommandDispatcher::Commands::CommandCallable::Errors
- Defined in:
- lib/simple_command_dispatcher/commands/errors.rb
Overview
Error collection for CommandCallable commands. Stores validation errors as a hash where keys are field names and values are arrays of error messages.
Instance Method Summary collapse
-
#add(key, value, _opts = {}) ⇒ Array
Adds an error message to the specified field.
-
#add_multiple_errors(errors_hash) ⇒ Object
Adds multiple errors from a hash.
-
#each {|field, message| ... } ⇒ Object
Iterates over each field and message pair.
-
#full_messages ⇒ Array<String>
Returns an array of formatted error messages.
Instance Method Details
#add(key, value, _opts = {}) ⇒ Array
Adds an error message to the specified field. Automatically prevents duplicate messages for the same field.
26 27 28 29 30 |
# File 'lib/simple_command_dispatcher/commands/errors.rb', line 26 def add(key, value, _opts = {}) self[key] ||= [] self[key] << value self[key].uniq! end |
#add_multiple_errors(errors_hash) ⇒ Object
Adds multiple errors from a hash. Values can be single messages or arrays of messages.
39 40 41 42 43 |
# File 'lib/simple_command_dispatcher/commands/errors.rb', line 39 def add_multiple_errors(errors_hash) errors_hash.each do |key, values| CommandCallable::Utils.array_wrap(values).each { |value| add key, value } end end |
#each {|field, message| ... } ⇒ Object
Iterates over each field and message pair. If a field has multiple messages, yields once for each message.
53 54 55 56 57 |
# File 'lib/simple_command_dispatcher/commands/errors.rb', line 53 def each each_key do |field| self[field].each { || yield field, } end end |
#full_messages ⇒ Array<String>
Returns an array of formatted error messages. Messages are prefixed with the capitalized field name, except for :base.
68 69 70 |
# File 'lib/simple_command_dispatcher/commands/errors.rb', line 68 def map { |attribute, | (attribute, ) } end |