Class: Dry::Schema::MessageSet
- Inherits:
-
Object
- Object
- Dry::Schema::MessageSet
- Includes:
- Extensions::Hints::MessageSetMethods, Enumerable
- Defined in:
- lib/dry/schema/message_set.rb
Overview
A set of messages used to generate errors
Instance Attribute Summary collapse
-
#messages ⇒ Array<Message>
readonly
A list of compiled message objects.
-
#options ⇒ Hash
readonly
Options hash.
-
#placeholders ⇒ Hash<Symbol=>[Array,Hash]>
readonly
An internal hash that is filled in with dumped messages when a message set is coerced to a hash.
Attributes included from Extensions::Hints::MessageSetMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ Array<String>
Get a list of message texts for the given key.
-
#each(&block) ⇒ Array
Iterate over messages.
-
#empty? ⇒ Boolean
Check if a message set is empty.
-
#fetch(key) ⇒ Array<String>
Get a list of message texts for the given key.
- #freeze ⇒ Object private
-
#initialize(messages, options = EMPTY_HASH) ⇒ MessageSet
constructor
private
A new instance of MessageSet.
-
#to_h ⇒ Hash<Symbol=>Array<String>>
(also: #to_hash)
Dump message set to a hash.
Constructor Details
#initialize(messages, options = EMPTY_HASH) ⇒ MessageSet
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of MessageSet.
38 39 40 41 42 |
# File 'lib/dry/schema/message_set.rb', line 38 def initialize(, = EMPTY_HASH) @messages = @options = initialize_placeholders! end |
Instance Attribute Details
#messages ⇒ Array<Message> (readonly)
A list of compiled message objects
19 20 21 |
# File 'lib/dry/schema/message_set.rb', line 19 def @messages end |
#options ⇒ Hash (readonly)
Options hash
30 31 32 |
# File 'lib/dry/schema/message_set.rb', line 30 def @options end |
#placeholders ⇒ Hash<Symbol=>[Array,Hash]> (readonly)
An internal hash that is filled in with dumped messages when a message set is coerced to a hash
25 26 27 |
# File 'lib/dry/schema/message_set.rb', line 25 def placeholders @placeholders end |
Class Method Details
.[](messages, options = EMPTY_HASH) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
33 34 35 |
# File 'lib/dry/schema/message_set.rb', line 33 def self.[](, = EMPTY_HASH) new(.flatten, ) end |
Instance Method Details
#[](key) ⇒ Array<String>
Get a list of message texts for the given key
78 79 80 |
# File 'lib/dry/schema/message_set.rb', line 78 def [](key) to_h[key] end |
#each(&block) ⇒ Array
Iterate over messages
54 55 56 57 58 59 |
# File 'lib/dry/schema/message_set.rb', line 54 def each(&block) return self if empty? return to_enum unless block .each(&block) end |
#empty? ⇒ Boolean
Check if a message set is empty
100 101 102 |
# File 'lib/dry/schema/message_set.rb', line 100 def empty? @empty ||= .empty? end |
#fetch(key) ⇒ Array<String>
Get a list of message texts for the given key
91 92 93 |
# File 'lib/dry/schema/message_set.rb', line 91 def fetch(key) self[key] || raise(KeyError, "+#{key}+ message was not found") end |
#freeze ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
105 106 107 108 109 |
# File 'lib/dry/schema/message_set.rb', line 105 def freeze to_h empty? super end |
#to_h ⇒ Hash<Symbol=>Array<String>> Also known as: to_hash
Dump message set to a hash
66 67 68 |
# File 'lib/dry/schema/message_set.rb', line 66 def to_h @to_h ||= end |