Class: Dry::Schema::MessageSet

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

Overview

A set of messages used to generate errors

See Also:

Instance Attribute Summary collapse

Attributes included from Extensions::Hints::MessageSetMethods

#failures, #hints

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.



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

def initialize(messages, options = EMPTY_HASH)
  @messages = messages
  @options = options
end

Instance Attribute Details

#messagesArray<Message> (readonly)

A list of compiled message objects

Returns:



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

def messages
  @messages
end

#optionsHash (readonly)

Options hash

Returns:

  • (Hash)


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

def options
  @options
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.



25
26
27
# File 'lib/dry/schema/message_set.rb', line 25

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

Instance Method Details

#[](key) ⇒ Array<String>

Get a list of message texts for the given key

Parameters:

  • key (Symbol)

Returns:

  • (Array<String>)


69
70
71
# File 'lib/dry/schema/message_set.rb', line 69

def [](key)
  to_h[key]
end

#each(&block) ⇒ Array

Iterate over messages

Examples:

result.errors.each do |message|
  puts message.text
end

Returns:

  • (Array)


45
46
47
48
49
50
# File 'lib/dry/schema/message_set.rb', line 45

def each(&block)
  return self if empty?
  return to_enum unless block

  messages.each(&block)
end

#empty?Boolean

Check if a message set is empty

Returns:

  • (Boolean)


91
92
93
# File 'lib/dry/schema/message_set.rb', line 91

def empty?
  @empty ||= messages.empty?
end

#fetch(key) ⇒ Array<String>

Get a list of message texts for the given key

Parameters:

  • key (Symbol)

Returns:

  • (Array<String>)

Raises:

  • KeyError



82
83
84
# File 'lib/dry/schema/message_set.rb', line 82

def fetch(key)
  self[key] || raise(KeyError, "+#{key}+ message was not found")
end

#freezeObject

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.



96
97
98
99
100
# File 'lib/dry/schema/message_set.rb', line 96

def freeze
  to_h
  empty?
  super
end

#to_hHash<Symbol=>Array<String>> Also known as: to_hash

Dump message set to a hash

Returns:

  • (Hash<Symbol=>Array<String>>)


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

def to_h
  @to_h ||= messages_map
end