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.



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

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

Instance Attribute Details

#messagesArray<Message> (readonly)

A list of compiled message objects

Returns:



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

def messages
  @messages
end

#optionsHash (readonly)

Options hash

Returns:

  • (Hash)


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

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.



27
28
29
# File 'lib/dry/schema/message_set.rb', line 27

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>)


71
72
73
# File 'lib/dry/schema/message_set.rb', line 71

def [](key)
  to_h[key]
end

#each(&block) ⇒ Array

Iterate over messages

Examples:

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

Returns:

  • (Array)


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

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)


93
94
95
# File 'lib/dry/schema/message_set.rb', line 93

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



84
85
86
# File 'lib/dry/schema/message_set.rb', line 84

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.



98
99
100
101
102
# File 'lib/dry/schema/message_set.rb', line 98

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>>)


59
60
61
# File 'lib/dry/schema/message_set.rb', line 59

def to_h
  @to_h ||= messages_map
end