Class: Dry::Validation::MessageSet

Inherits:
Object
  • Object
show all
Includes:
Core::Constants, 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?).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.



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

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.



11
12
13
# File 'lib/dry/validation/message_set.rb', line 11

def failures
  @failures
end

#hintsObject (readonly)

Returns the value of attribute hints.



11
12
13
# File 'lib/dry/validation/message_set.rb', line 11

def hints
  @hints
end

#messagesObject (readonly)

Returns the value of attribute messages.



11
12
13
# File 'lib/dry/validation/message_set.rb', line 11

def messages
  @messages
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/dry/validation/message_set.rb', line 11

def options
  @options
end

#pathsObject (readonly)

Returns the value of attribute paths.



11
12
13
# File 'lib/dry/validation/message_set.rb', line 11

def paths
  @paths
end

#placeholdersObject (readonly)

Returns the value of attribute placeholders.



11
12
13
# File 'lib/dry/validation/message_set.rb', line 11

def placeholders
  @placeholders
end

Class Method Details

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



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

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

Instance Method Details

#dumpObject



28
29
30
# File 'lib/dry/validation/message_set.rb', line 28

def dump
  root? ? to_a : to_h
end

#each(&block) ⇒ Object



44
45
46
47
# File 'lib/dry/validation/message_set.rb', line 44

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

#empty?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/dry/validation/message_set.rb', line 36

def empty?
  messages.empty?
end

#failures?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/dry/validation/message_set.rb', line 32

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

#root?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/dry/validation/message_set.rb', line 40

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

#to_aObject



58
59
60
# File 'lib/dry/validation/message_set.rb', line 58

def to_a
  to_h.values.flatten
end

#to_hObject Also known as: to_hash



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

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