Class: Dry::Validation::MessageSet
- Inherits:
-
Object
- Object
- Dry::Validation::MessageSet
- Includes:
- Enumerable
- Defined in:
- lib/dry/validation/message_set.rb
Instance Attribute Summary collapse
-
#hints ⇒ Object
readonly
Returns the value of attribute hints.
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#paths ⇒ Object
readonly
Returns the value of attribute paths.
-
#placeholders ⇒ Object
readonly
Returns the value of attribute placeholders.
Class Method Summary collapse
Instance Method Summary collapse
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(messages) ⇒ MessageSet
constructor
A new instance of MessageSet.
- #to_h ⇒ Object (also: #to_hash)
- #with_hints!(hints) ⇒ Object
Constructor Details
#initialize(messages) ⇒ MessageSet
Returns a new instance of MessageSet.
14 15 16 17 18 19 |
# File 'lib/dry/validation/message_set.rb', line 14 def initialize() @messages = @hints = {} @paths = map(&:path).uniq initialize_placeholders! end |
Instance Attribute Details
#hints ⇒ Object (readonly)
Returns the value of attribute hints.
8 9 10 |
# File 'lib/dry/validation/message_set.rb', line 8 def hints @hints end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
8 9 10 |
# File 'lib/dry/validation/message_set.rb', line 8 def @messages end |
#paths ⇒ Object (readonly)
Returns the value of attribute paths.
8 9 10 |
# File 'lib/dry/validation/message_set.rb', line 8 def paths @paths end |
#placeholders ⇒ Object (readonly)
Returns the value of attribute placeholders.
8 9 10 |
# File 'lib/dry/validation/message_set.rb', line 8 def placeholders @placeholders end |
Class Method Details
.[](messages) ⇒ Object
10 11 12 |
# File 'lib/dry/validation/message_set.rb', line 10 def self.[]() new(.flatten) end |
Instance Method Details
#each(&block) ⇒ Object
25 26 27 28 |
# File 'lib/dry/validation/message_set.rb', line 25 def each(&block) return to_enum unless block .each(&block) end |
#empty? ⇒ Boolean
21 22 23 |
# File 'lib/dry/validation/message_set.rb', line 21 def empty? .empty? end |
#to_h ⇒ Object Also known as: to_hash
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/dry/validation/message_set.rb', line 35 def to_h reduce(placeholders) do |hash, msg| if msg.root? (hash[nil] ||= []) << msg.to_s else node = msg.path.reduce(hash) { |a, e| a[e] } node << msg node.concat(Array(hints[msg.index_path])) node.uniq!(&:signature) node.map!(&:to_s) end hash end end |
#with_hints!(hints) ⇒ Object
30 31 32 33 |
# File 'lib/dry/validation/message_set.rb', line 30 def with_hints!(hints) @hints = hints.group_by(&:index_path) freeze end |