Class: Dry::Schema::Result

Inherits:
Object
  • Object
show all
Extended by:
Initializer
Includes:
Monads::Result::Mixin
Defined in:
lib/dry/schema/result.rb,
lib/dry/schema/extensions/monads.rb

Overview

Processing result

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(*args) {|result| ... } ⇒ 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.

Yields:

  • (result)


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

def self.new(*args)
  result = super
  yield(result)
  result.freeze
end

Instance Method Details

#[](name) ⇒ Object

Read value from the output hash

Parameters:

  • name (Symbol)

Returns:

  • (Object)


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

def [](name)
  output[name]
end

#concat(other) ⇒ 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.



43
44
45
46
47
# File 'lib/dry/schema/result.rb', line 43

def concat(other)
  results.concat(other)
  result_ast.concat(other.map(&:to_ast))
  self
end

#error?(spec) ⇒ Boolean

Check if there’s an error for the provided spec

Parameters:

  • name (Symbol, Hash<Symbol=>Symbol>)

Returns:

  • (Boolean)


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

def error?(spec)
  message_set.any? { |msg| Path[spec] == msg.path }
end

#errors(options = EMPTY_HASH) ⇒ Hash<Symbol=>Array>

Get human-readable error representation

Returns:

  • (Hash<Symbol=>Array>)

See Also:



107
108
109
# File 'lib/dry/schema/result.rb', line 107

def errors(options = EMPTY_HASH)
  message_set(options.merge(hints: false)).dump
end

#failure?Boolean

Check if the result is not successful

Returns:

  • (Boolean)


96
97
98
# File 'lib/dry/schema/result.rb', line 96

def failure?
  !success?
end

#hints(options = EMPTY_HASH) ⇒ Hash<Symbol=>Array>

Get hints exclusively without errors

Returns:

  • (Hash<Symbol=>Array>)

See Also:



129
130
131
# File 'lib/dry/schema/result.rb', line 129

def hints(options = EMPTY_HASH)
  message_set(options.merge(failures: false)).dump
end

#inspectString

Return a string representation of the result

Returns:

  • (String)


152
153
154
# File 'lib/dry/schema/result.rb', line 152

def inspect
  "#<#{self.class}#{to_h.inspect} errors=#{errors.inspect}>"
end

#key?(name) ⇒ Boolean

Check if a given key is present in the output

Parameters:

  • name (Symbol)

Returns:

  • (Boolean)


67
68
69
# File 'lib/dry/schema/result.rb', line 67

def key?(name)
  output.key?(name)
end

#message_set(options = EMPTY_HASH) ⇒ MessageSet

Return the message set

Parameters:

  • options (Hash) (defaults to: EMPTY_HASH)

Options Hash (options):

  • :locale (Symbol)

    Alternative locale (default is :en)

  • :hints (Boolean)

    Whether to include hint messages or not

  • :full (Boolean)

    Whether to generate messages that include key names

Returns:



143
144
145
# File 'lib/dry/schema/result.rb', line 143

def message_set(options = EMPTY_HASH)
  message_compiler.with(options).(result_ast)
end

#messages(options = EMPTY_HASH) ⇒ Hash<Symbol=>Array>

Get all messages including hints

Returns:

  • (Hash<Symbol=>Array>)

See Also:



118
119
120
# File 'lib/dry/schema/result.rb', line 118

def messages(options = EMPTY_HASH)
  message_set(options.merge(hints: true)).dump
end

#replace(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.



37
38
39
40
# File 'lib/dry/schema/result.rb', line 37

def replace(hash)
  @output = hash
  self
end

#success?Boolean

Check if the result is successful

Returns:

  • (Boolean)


87
88
89
# File 'lib/dry/schema/result.rb', line 87

def success?
  results.empty?
end

#to_monad(options = EMPTY_HASH) ⇒ Object Also known as: to_result



8
9
10
11
12
13
14
# File 'lib/dry/schema/extensions/monads.rb', line 8

def to_monad(options = EMPTY_HASH)
  if success?
    Success(output)
  else
    Failure(messages(options))
  end
end