Class: Dry::Schema::MessageSet

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

Overview

A set of messages used to generate errors

See Also:

Constant Summary collapse

HINT_EXCLUSION =
i(
  key? filled? nil? 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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of MessageSet.



26
27
28
29
30
31
32
33
34
35
# File 'lib/dry/schema/message_set.rb', line 26

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)



18
19
20
# File 'lib/dry/schema/message_set.rb', line 18

def failures
  @failures
end

#hintsObject (readonly)



18
19
20
# File 'lib/dry/schema/message_set.rb', line 18

def hints
  @hints
end

#messagesObject (readonly)



18
19
20
# File 'lib/dry/schema/message_set.rb', line 18

def messages
  @messages
end

#optionsObject (readonly)



18
19
20
# File 'lib/dry/schema/message_set.rb', line 18

def options
  @options
end

#pathsObject (readonly)



18
19
20
# File 'lib/dry/schema/message_set.rb', line 18

def paths
  @paths
end

#placeholdersObject (readonly)



18
19
20
# File 'lib/dry/schema/message_set.rb', line 18

def placeholders
  @placeholders
end

Class Method Details

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

Instance Method Details

#each(&block) ⇒ Object



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

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

#empty?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


56
57
58
# File 'lib/dry/schema/message_set.rb', line 56

def empty?
  messages.empty?
end

#failures?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


51
52
53
# File 'lib/dry/schema/message_set.rb', line 51

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

#to_hObject Also known as: to_hash, dump



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

def to_h
  failures? ? messages_map : hints_map
end