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 ‘message` under `key`.
-
#added?(key, message) ⇒ Boolean
True when ‘message` is recorded under `key`.
-
#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 ‘other` into 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.
46 47 48 |
# File 'lib/cmdx/errors.rb', line 46 def [](key) [key.to_sym]&.to_a || EMPTY_ARRAY end |
#add(key, message) ⇒ Set<String> Also known as: []=
Adds ‘message` under `key`. Duplicate messages are silently dropped. `key` is coerced to a Symbol to match Context’s key normalization.
23 24 25 |
# File 'lib/cmdx/errors.rb', line 23 def add(key, ) ([key.to_sym] ||= Set.new) << end |
#added?(key, message) ⇒ Boolean
Returns true when ‘message` is recorded under `key`.
53 54 55 |
# File 'lib/cmdx/errors.rb', line 53 def added?(key, ) !![key.to_sym]&.include?() end |
#as_json ⇒ Hash{Symbol => Array<String>}
JSON-friendly hash view. Aliases #to_h for conventional ‘as_json` callers (e.g. Rails).
139 140 141 |
# File 'lib/cmdx/errors.rb', line 139 def as_json(*) to_h end |
#clear ⇒ Hash{Symbol => Set<String>}
Returns empties the container.
109 110 111 |
# File 'lib/cmdx/errors.rb', line 109 def clear .clear end |
#count ⇒ Integer
Returns total messages across all keys.
80 81 82 |
# File 'lib/cmdx/errors.rb', line 80 def count .each_value.sum(&:size) end |
#deconstruct ⇒ Array<Array(Symbol, Array<String>)>
Pattern-matching support for ‘case errors in […]`.
174 175 176 |
# File 'lib/cmdx/errors.rb', line 174 def deconstruct to_h.to_a end |
#deconstruct_keys(keys) ⇒ Hash{Symbol => Array<String>}
Pattern-matching support for ‘case errors in …`.
167 168 169 |
# File 'lib/cmdx/errors.rb', line 167 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.
104 105 106 |
# File 'lib/cmdx/errors.rb', line 104 def delete(key) .delete(key.to_sym) end |
#each {|key, set| ... } ⇒ Errors, Enumerator
86 87 88 |
# File 'lib/cmdx/errors.rb', line 86 def each(&) .each(&) end |
#each_key {|Symbol| ... } ⇒ Errors, Enumerator
92 93 94 |
# File 'lib/cmdx/errors.rb', line 92 def each_key(&) .each_key(&) end |
#each_value {|Set<String>| ... } ⇒ Errors, Enumerator
98 99 100 |
# File 'lib/cmdx/errors.rb', line 98 def each_value(&) .each_value(&) end |
#empty? ⇒ Boolean
70 71 72 |
# File 'lib/cmdx/errors.rb', line 70 def empty? .empty? end |
#freeze ⇒ Errors
Freezes the container and every message set. Called by Runtime teardown.
181 182 183 184 |
# File 'lib/cmdx/errors.rb', line 181 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”] }`).
115 116 117 118 119 120 121 122 |
# File 'lib/cmdx/errors.rb', line 115 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?
59 60 61 |
# File 'lib/cmdx/errors.rb', line 59 def key?(key) .key?(key.to_sym) end |
#keys ⇒ Array<Symbol>
Returns keys with at least one message.
65 66 67 |
# File 'lib/cmdx/errors.rb', line 65 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=> Enumerable<String>` — typically another CMDx::Errors instance.
38 39 40 41 42 |
# File 'lib/cmdx/errors.rb', line 38 def merge!(other) other.to_hash.each do |key, | .each { || add(key, ) } end end |
#size ⇒ Integer
Returns number of keyed entries.
75 76 77 |
# File 'lib/cmdx/errors.rb', line 75 def size .size end |
#to_h ⇒ Hash{Symbol => Array<String>}
Returns raw messages as arrays.
125 126 127 |
# File 'lib/cmdx/errors.rb', line 125 def to_h .transform_values(&:to_a) end |
#to_hash(full = false) ⇒ Hash{Symbol => Array<String>}
131 132 133 |
# File 'lib/cmdx/errors.rb', line 131 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.
148 149 150 |
# File 'lib/cmdx/errors.rb', line 148 def to_json(*args) to_h.to_json(*args) end |
#to_s ⇒ String
Returns all full messages joined with ‘“. ”`, suitable as a fail reason.
154 155 156 |
# File 'lib/cmdx/errors.rb', line 154 def to_s .values.flatten.join(". ") end |