Class: CMDx::Errors
Overview
Per-task container of validation / coercion / output errors. Each key maps
to a deduplicating Set of messages. A non-empty Errors forces Runtime to
throw a failed signal (signal_errors!). Frozen on teardown by Runtime.
Instance Attribute Summary collapse
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
Instance Method Summary collapse
-
#[](key) ⇒ Array<String>
Messages for
key, or a frozen empty array. -
#add(key, message) ⇒ Set<String>
(also: #[]=)
Adds
messageunderkey. -
#added?(key, message) ⇒ Boolean
True when
messageis recorded underkey. -
#as_json ⇒ Hash{Symbol => Array<String>}
JSON-friendly hash view.
-
#clear ⇒ Hash{Symbol => Set<String>}
Empties the container.
-
#count ⇒ Integer
Total messages across all keys.
-
#deconstruct ⇒ Array<Array(Symbol, Array<String>)>
Pattern-matching support for
case errors in [...]. -
#deconstruct_keys(keys) ⇒ Hash{Symbol => Array<String>}
Pattern-matching support for
case errors in {...}. -
#delete(key) ⇒ Set<String>?
The removed set, or nil when absent.
- #each {|key, set| ... } ⇒ Errors, Enumerator
- #each_key {|Symbol| ... } ⇒ Errors, Enumerator
- #each_value {|Set<String>| ... } ⇒ Errors, Enumerator
- #empty? ⇒ Boolean
-
#freeze ⇒ Errors
Freezes the container and every message set.
-
#full_messages ⇒ Hash{Symbol => Array<String>}
Messages prefixed with their key (e.g.
{ name: ["name is required"] }). -
#initialize ⇒ Errors
constructor
A new instance of Errors.
- #key?(key) ⇒ Boolean (also: #for?)
-
#keys ⇒ Array<Symbol>
Keys with at least one message.
-
#merge!(other) ⇒ void
Copies every message from
otherinto self. -
#size ⇒ Integer
Number of keyed entries.
-
#to_h ⇒ Hash{Symbol => Array<String>}
Raw messages as arrays.
- #to_hash(full = false) ⇒ Hash{Symbol => Array<String>}
-
#to_json(*args) ⇒ String
Serializes the error messages to a JSON string.
-
#to_s ⇒ String
All full messages joined with `".
Constructor Details
#initialize ⇒ Errors
Returns a new instance of Errors.
13 14 15 |
# File 'lib/cmdx/errors.rb', line 13 def initialize @messages = {} end |
Instance Attribute Details
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
11 12 13 |
# File 'lib/cmdx/errors.rb', line 11 def @messages end |
Instance Method Details
#[](key) ⇒ Array<String>
Returns messages for key, or a frozen empty array.
45 46 47 |
# File 'lib/cmdx/errors.rb', line 45 def [](key) [key]&.to_a || EMPTY_ARRAY end |
#add(key, message) ⇒ Set<String> Also known as: []=
Adds message under key. Duplicate messages are silently dropped.
22 23 24 |
# File 'lib/cmdx/errors.rb', line 22 def add(key, ) ([key] ||= Set.new) << end |
#added?(key, message) ⇒ Boolean
Returns true when message is recorded under key.
52 53 54 |
# File 'lib/cmdx/errors.rb', line 52 def added?(key, ) !![key]&.include?() end |
#as_json ⇒ Hash{Symbol => Array<String>}
JSON-friendly hash view. Aliases #to_h for conventional as_json
callers (e.g. Rails).
138 139 140 |
# File 'lib/cmdx/errors.rb', line 138 def as_json(*) to_h end |
#clear ⇒ Hash{Symbol => Set<String>}
Returns empties the container.
108 109 110 |
# File 'lib/cmdx/errors.rb', line 108 def clear .clear end |
#count ⇒ Integer
Returns total messages across all keys.
79 80 81 |
# File 'lib/cmdx/errors.rb', line 79 def count .each_value.sum(&:size) end |
#deconstruct ⇒ Array<Array(Symbol, Array<String>)>
Pattern-matching support for case errors in [...].
173 174 175 |
# File 'lib/cmdx/errors.rb', line 173 def deconstruct to_h.to_a end |
#deconstruct_keys(keys) ⇒ Hash{Symbol => Array<String>}
Pattern-matching support for case errors in {...}.
166 167 168 |
# File 'lib/cmdx/errors.rb', line 166 def deconstruct_keys(keys) keys.nil? ? to_h : to_h.slice(*keys) end |
#delete(key) ⇒ Set<String>?
Returns the removed set, or nil when absent.
103 104 105 |
# File 'lib/cmdx/errors.rb', line 103 def delete(key) .delete(key) end |
#each {|key, set| ... } ⇒ Errors, Enumerator
85 86 87 |
# File 'lib/cmdx/errors.rb', line 85 def each(&) .each(&) end |
#each_key {|Symbol| ... } ⇒ Errors, Enumerator
91 92 93 |
# File 'lib/cmdx/errors.rb', line 91 def each_key(&) .each_key(&) end |
#each_value {|Set<String>| ... } ⇒ Errors, Enumerator
97 98 99 |
# File 'lib/cmdx/errors.rb', line 97 def each_value(&) .each_value(&) end |
#empty? ⇒ Boolean
69 70 71 |
# File 'lib/cmdx/errors.rb', line 69 def empty? .empty? end |
#freeze ⇒ Errors
Freezes the container and every message set. Called by Runtime teardown.
180 181 182 183 |
# File 'lib/cmdx/errors.rb', line 180 def freeze .each_value(&:freeze).freeze super end |
#full_messages ⇒ Hash{Symbol => Array<String>}
Returns messages prefixed with their key
(e.g. { name: ["name is required"] }).
114 115 116 117 118 119 120 121 |
# File 'lib/cmdx/errors.rb', line 114 def .each_with_object({}) do |(key, set), hash| hash[key] = set.map do || = I18nProxy.t(, default: ) "#{key} #{}" end end end |
#key?(key) ⇒ Boolean Also known as: for?
58 59 60 |
# File 'lib/cmdx/errors.rb', line 58 def key?(key) .key?(key) end |
#keys ⇒ Array<Symbol>
Returns keys with at least one message.
64 65 66 |
# File 'lib/cmdx/errors.rb', line 64 def keys .keys end |
#merge!(other) ⇒ void
This method returns an undefined value.
Copies every message from other into self. Existing messages are
preserved and duplicates (same key + message) are silently dropped by
the underlying Set. Accepts any object that responds to #to_hash
returning Hash{Symbol => Enumerable<String>} — typically another
CMDx::Errors instance.
37 38 39 40 41 |
# File 'lib/cmdx/errors.rb', line 37 def merge!(other) other.to_hash.each do |key, | .each { || add(key, ) } end end |
#size ⇒ Integer
Returns number of keyed entries.
74 75 76 |
# File 'lib/cmdx/errors.rb', line 74 def size .size end |
#to_h ⇒ Hash{Symbol => Array<String>}
Returns raw messages as arrays.
124 125 126 |
# File 'lib/cmdx/errors.rb', line 124 def to_h .transform_values(&:to_a) end |
#to_hash(full = false) ⇒ Hash{Symbol => Array<String>}
130 131 132 |
# File 'lib/cmdx/errors.rb', line 130 def to_hash(full = false) full ? : to_h end |
#to_json(*args) ⇒ String
Serializes the error messages to a JSON string. Symbol keys are
emitted as strings by the json stdlib.
147 148 149 |
# File 'lib/cmdx/errors.rb', line 147 def to_json(*args) to_h.to_json(*args) end |
#to_s ⇒ String
Returns all full messages joined with ". ", suitable as a
fail reason.
153 154 155 |
# File 'lib/cmdx/errors.rb', line 153 def to_s .values.flatten.join(". ") end |