Class: CMDx::Errors
- Inherits:
-
Object
- Object
- CMDx::Errors
- Extended by:
- Forwardable
- Defined in:
- lib/cmdx/errors.rb
Overview
Collection of validation and execution errors organized by attribute. Provides methods to add, query, and format error messages for different attributes in a task or workflow execution.
Instance Attribute Summary collapse
-
#messages ⇒ Hash{Symbol => Set<String>}
readonly
Returns the internal hash of error messages by attribute.
Instance Method Summary collapse
-
#add(attribute, message) ⇒ Object
Add an error message for a specific attribute.
-
#for?(attribute) ⇒ Boolean
Check if there are any errors for a specific attribute.
-
#full_messages ⇒ Hash{Symbol => Array<String>}
Convert errors to a hash format with arrays of full messages.
-
#initialize ⇒ Errors
constructor
Initialize a new error collection.
-
#to_h ⇒ Hash{Symbol => Array<String>}
Convert errors to a hash format with arrays of messages.
-
#to_hash(full = false) ⇒ Hash{Symbol => Array<String>}
Convert errors to a hash format with optional full messages.
-
#to_s ⇒ String
Convert errors to a human-readable string format.
Constructor Details
#initialize ⇒ Errors
Initialize a new error collection.
26 27 28 |
# File 'lib/cmdx/errors.rb', line 26 def initialize @messages = {} end |
Instance Attribute Details
#messages ⇒ Hash{Symbol => Set<String>} (readonly)
Returns the internal hash of error messages by attribute.
19 20 21 |
# File 'lib/cmdx/errors.rb', line 19 def @messages end |
Instance Method Details
#add(attribute, message) ⇒ Object
Add an error message for a specific attribute.
41 42 43 44 45 46 |
# File 'lib/cmdx/errors.rb', line 41 def add(attribute, ) return if .empty? [attribute] ||= Set.new [attribute] << end |
#for?(attribute) ⇒ Boolean
Check if there are any errors for a specific attribute.
59 60 61 62 63 |
# File 'lib/cmdx/errors.rb', line 59 def for?(attribute) return false unless .key?(attribute) ![attribute].empty? end |
#full_messages ⇒ Hash{Symbol => Array<String>}
Convert errors to a hash format with arrays of full messages.
73 74 75 76 77 |
# File 'lib/cmdx/errors.rb', line 73 def .each_with_object({}) do |(attribute, ), hash| hash[attribute] = .map { || "#{attribute} #{}" } end end |
#to_h ⇒ Hash{Symbol => Array<String>}
Convert errors to a hash format with arrays of messages.
87 88 89 |
# File 'lib/cmdx/errors.rb', line 87 def to_h .transform_values(&:to_a) end |
#to_hash(full = false) ⇒ Hash{Symbol => Array<String>}
Convert errors to a hash format with optional full messages.
101 102 103 |
# File 'lib/cmdx/errors.rb', line 101 def to_hash(full = false) full ? : to_h end |
#to_s ⇒ String
Convert errors to a human-readable string format.
113 114 115 |
# File 'lib/cmdx/errors.rb', line 113 def to_s .values.flatten.join(". ") end |