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 |
# File 'lib/cmdx/errors.rb', line 59 def for?(attribute) set = [attribute] !set.nil? && !set.empty? end |
#full_messages ⇒ Hash{Symbol => Array<String>}
Convert errors to a hash format with arrays of full messages.
72 73 74 75 76 |
# File 'lib/cmdx/errors.rb', line 72 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.
86 87 88 |
# File 'lib/cmdx/errors.rb', line 86 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.
100 101 102 |
# File 'lib/cmdx/errors.rb', line 100 def to_hash(full = false) full ? : to_h end |
#to_s ⇒ String
Convert errors to a human-readable string format.
112 113 114 |
# File 'lib/cmdx/errors.rb', line 112 def to_s .values.flatten.join(". ") end |