Class: Dry::Validation::Rust::MessageSet
- Inherits:
-
Object
- Object
- Dry::Validation::Rust::MessageSet
- Includes:
- Enumerable
- Defined in:
- lib/dry/validation/rust/message_set.rb
Overview
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
Rendering options applied to this message set.
Instance Method Summary collapse
-
#[](key) ⇒ MessageSet
Returns messages at or below a key or path.
-
#add(message) ⇒ MessageSet
Adds a message and invalidates derived views.
-
#each {|message| ... } ⇒ Enumerator, MessageSet
Iterates over messages.
-
#empty? ⇒ Boolean
Returns whether this set contains no messages.
-
#filter(*predicates) ⇒ MessageSet
Returns messages satisfying every supplied message predicate.
-
#freeze ⇒ MessageSet
Freezes this set and its cached derived Hash.
-
#initialize(messages = [], options = {}) ⇒ MessageSet
constructor
Creates a message set from messages and optional rendering options.
-
#messages ⇒ Array<Message>
Returns an immutable snapshot of the messages.
-
#to_h ⇒ Hash
Returns messages grouped into a nested Hash by path.
-
#with(new_options = {}) ⇒ MessageSet
Returns a copy with rendering options, including full-message text.
Constructor Details
#initialize(messages = [], options = {}) ⇒ MessageSet
Creates a message set from messages and optional rendering options.
25 26 27 28 |
# File 'lib/dry/validation/rust/message_set.rb', line 25 def initialize( = [], = {}) @messages = .dup @options = end |
Instance Attribute Details
#options ⇒ Hash (readonly)
Returns rendering options applied to this message set.
18 19 20 |
# File 'lib/dry/validation/rust/message_set.rb', line 18 def @options end |
Instance Method Details
#[](key) ⇒ MessageSet
Returns messages at or below a key or path.
54 55 56 57 |
# File 'lib/dry/validation/rust/message_set.rb', line 54 def [](key) wanted = Path.parse(key) self.class.new(@messages.select { || Path.prefix?(.path, wanted) }, ) end |
#add(message) ⇒ MessageSet
Adds a message and invalidates derived views.
63 64 65 66 67 68 |
# File 'lib/dry/validation/rust/message_set.rb', line 63 def add() @messages << @readonly_messages = nil @to_h = nil self end |
#each {|message| ... } ⇒ Enumerator, MessageSet
Iterates over messages.
37 38 39 |
# File 'lib/dry/validation/rust/message_set.rb', line 37 def each(&) @messages.each(&) end |
#empty? ⇒ Boolean
Returns whether this set contains no messages.
73 74 75 |
# File 'lib/dry/validation/rust/message_set.rb', line 73 def empty? @messages.empty? end |
#filter(*predicates) ⇒ MessageSet
Returns messages satisfying every supplied message predicate.
Each predicate must be a public predicate method on a message and return a truthy value. A message without a requested predicate is excluded.
87 88 89 90 91 92 93 94 |
# File 'lib/dry/validation/rust/message_set.rb', line 87 def filter(*predicates) self.class.new( @messages.select do || predicates.all? { |predicate| .respond_to?(predicate) && .public_send(predicate) } end, ) end |
#freeze ⇒ MessageSet
Freezes this set and its cached derived Hash.
125 126 127 128 129 130 |
# File 'lib/dry/validation/rust/message_set.rb', line 125 def freeze @messages.freeze # Keep the derived representation so callers of a frozen set reuse it. to_h.freeze super end |
#messages ⇒ Array<Message>
Returns an immutable snapshot of the messages.
44 45 46 |
# File 'lib/dry/validation/rust/message_set.rb', line 44 def end |
#to_h ⇒ Hash
Returns messages grouped into a nested Hash by path.
118 119 120 |
# File 'lib/dry/validation/rust/message_set.rb', line 118 def to_h @to_h ||= build_nested_hash end |
#with(new_options = {}) ⇒ MessageSet
Returns a copy with rendering options, including full-message text.
With full: true, non-base message text is prefixed with its
humanized path (for example, :first_name becomes "first name").
106 107 108 109 110 111 |
# File 'lib/dry/validation/rust/message_set.rb', line 106 def with( = {}) merged = .merge() return self.class.new(@messages, merged) unless merged[:full] self.class.new(@messages.map { || () }, merged) end |