Class: Dry::Validation::MessageSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dry/validation/message_set.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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)
  @messages = messages
  @hints = {}
  @paths = map(&:path).uniq
  initialize_placeholders!
end

Instance Attribute Details

#hintsObject (readonly)

Returns the value of attribute hints.



8
9
10
# File 'lib/dry/validation/message_set.rb', line 8

def hints
  @hints
end

#messagesObject (readonly)

Returns the value of attribute messages.



8
9
10
# File 'lib/dry/validation/message_set.rb', line 8

def messages
  @messages
end

#pathsObject (readonly)

Returns the value of attribute paths.



8
9
10
# File 'lib/dry/validation/message_set.rb', line 8

def paths
  @paths
end

#placeholdersObject (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.[](messages)
  new(messages.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
  messages.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/dry/validation/message_set.rb', line 21

def empty?
  messages.empty?
end

#to_hObject 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