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.



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

def initialize(messages, options = EMPTY_HASH)
  @messages = messages
  @options = options
  initialize_placeholders!
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)


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

def options
  @options
end

#placeholdersHash<Symbol=>[Array,Hash]> (readonly)

An internal hash that is filled in with dumped messages when a message set is coerced to a hash

Returns:

  • (Hash<Symbol=>[Array,Hash]>)


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

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.



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

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


78
79
80
# File 'lib/dry/schema/message_set.rb', line 78

def [](key)
  to_h[key]
end

#each(&block) ⇒ Array

Iterate over messages

Examples:

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

Returns:

  • (Array)


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

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)


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

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



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

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.



105
106
107
108
109
# File 'lib/dry/schema/message_set.rb', line 105

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


66
67
68
# File 'lib/dry/schema/message_set.rb', line 66

def to_h
  @to_h ||= messages_map
end