Class: Dry::Validation::MessageSet

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

Constant Summary collapse

HINT_EXCLUSION =
%i(
  key? filled? none? bool?
  str? int? float? decimal?
  date? date_time? time? hash?
  array? format?
).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(messages, options = EMPTY_HASH) ⇒ MessageSet

Returns a new instance of MessageSet.



19
20
21
22
23
24
25
26
27
28
# File 'lib/dry/validation/message_set.rb', line 19

def initialize(messages, options = EMPTY_HASH)
  @messages = messages
  @hints = messages.select(&:hint?)
  @failures = messages - hints
  @paths = failures.map(&:path).uniq
  @options = options

  initialize_hints!
  initialize_placeholders!
end

Instance Attribute Details

#failuresObject (readonly)

Returns the value of attribute failures.



13
14
15
# File 'lib/dry/validation/message_set.rb', line 13

def failures
  @failures
end

#hintsObject (readonly)

Returns the value of attribute hints.



13
14
15
# File 'lib/dry/validation/message_set.rb', line 13

def hints
  @hints
end

#messagesObject (readonly)

Returns the value of attribute messages.



13
14
15
# File 'lib/dry/validation/message_set.rb', line 13

def messages
  @messages
end

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/dry/validation/message_set.rb', line 13

def options
  @options
end

#pathsObject (readonly)

Returns the value of attribute paths.



13
14
15
# File 'lib/dry/validation/message_set.rb', line 13

def paths
  @paths
end

#placeholdersObject (readonly)

Returns the value of attribute placeholders.



13
14
15
# File 'lib/dry/validation/message_set.rb', line 13

def placeholders
  @placeholders
end

Class Method Details

.[](messages, options = EMPTY_HASH) ⇒ Object



15
16
17
# File 'lib/dry/validation/message_set.rb', line 15

def self.[](messages, options = EMPTY_HASH)
  new(messages.flatten, options)
end

Instance Method Details

#dumpObject



30
31
32
# File 'lib/dry/validation/message_set.rb', line 30

def dump
  root? ? to_a : to_h
end

#each(&block) ⇒ Object



46
47
48
49
# File 'lib/dry/validation/message_set.rb', line 46

def each(&block)
  return to_enum unless block
  messages.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/dry/validation/message_set.rb', line 38

def empty?
  messages.empty?
end

#failures?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/dry/validation/message_set.rb', line 34

def failures?
  options[:failures].equal?(true)
end

#root?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/dry/validation/message_set.rb', line 42

def root?
  !empty? && failures.all?(&:root?)
end

#to_aObject



60
61
62
# File 'lib/dry/validation/message_set.rb', line 60

def to_a
  to_h.values.flatten
end

#to_hObject Also known as: to_hash



51
52
53
54
55
56
57
# File 'lib/dry/validation/message_set.rb', line 51

def to_h
  if root?
    { nil => failures.map(&:to_s) }
  else
    failures? ? messages_map : hints_map
  end
end