Class: Dry::Schema::Result

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

Overview

Monad extension for Result

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Extensions::Hints::ResultMethods

#hints, #messages

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)


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

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)


61
62
63
# File 'lib/dry/schema/result.rb', line 61

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.



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

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:

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

Returns:

  • (Boolean)


83
84
85
# File 'lib/dry/schema/result.rb', line 83

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

#errors(options = EMPTY_HASH) ⇒ MessageSet

Get human-readable error representation

Returns:

See Also:



112
113
114
# File 'lib/dry/schema/result.rb', line 112

def errors(options = EMPTY_HASH)
  message_set(options)
end

#failure?Boolean

Check if the result is not successful

Returns:

  • (Boolean)


101
102
103
# File 'lib/dry/schema/result.rb', line 101

def failure?
  !success?
end

#inspectString

Return a string representation of the result

Returns:

  • (String)


135
136
137
# File 'lib/dry/schema/result.rb', line 135

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

#key?(name) ⇒ Boolean

Check if a given key is present in the output

Parameters:

  • name (Symbol)

Returns:

  • (Boolean)


72
73
74
# File 'lib/dry/schema/result.rb', line 72

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:



126
127
128
# File 'lib/dry/schema/result.rb', line 126

def message_set(options = EMPTY_HASH)
  message_compiler.with(options).(result_ast)
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.



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

def replace(hash)
  @output = hash
  self
end

#success?Boolean

Check if the result is successful

Returns:

  • (Boolean)


92
93
94
# File 'lib/dry/schema/result.rb', line 92

def success?
  results.empty?
end

#to_monadDry::Monads::Success, Dry::Monads::Failure

Turn result into a monad

This makes result objects work with dry-monads (or anything with a compatible interface)

Returns:

  • (Dry::Monads::Success, Dry::Monads::Failure)


20
21
22
23
24
25
26
# File 'lib/dry/schema/extensions/monads.rb', line 20

def to_monad
  if success?
    Success(self)
  else
    Failure(self)
  end
end