Class: Dry::Validation::Result

Inherits:
Object
  • Object
show all
Includes:
Core::Constants, Monads::Either::Mixin, Enumerable
Defined in:
lib/dry/validation/result.rb,
lib/dry/validation/extensions/monads.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, results, message_compiler, path) ⇒ Result

Returns a new instance of Result.



18
19
20
21
22
23
# File 'lib/dry/validation/result.rb', line 18

def initialize(output, results, message_compiler, path)
  @output = output
  @results = results
  @message_compiler = message_compiler
  @path = path
end

Instance Attribute Details

#message_compilerObject (readonly)

Returns the value of attribute message_compiler.



12
13
14
# File 'lib/dry/validation/result.rb', line 12

def message_compiler
  @message_compiler
end

#outputObject (readonly) Also known as: to_hash, to_h

Returns the value of attribute output.



10
11
12
# File 'lib/dry/validation/result.rb', line 10

def output
  @output
end

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/dry/validation/result.rb', line 13

def path
  @path
end

#resultsObject (readonly)

Returns the value of attribute results.



11
12
13
# File 'lib/dry/validation/result.rb', line 11

def results
  @results
end

Instance Method Details

#[](name) ⇒ Object



29
30
31
# File 'lib/dry/validation/result.rb', line 29

def [](name)
  output.fetch(name)
end

#astObject



65
66
67
# File 'lib/dry/validation/result.rb', line 65

def ast(*)
  [:set, result_ast]
end

#each(&block) ⇒ Object



25
26
27
# File 'lib/dry/validation/result.rb', line 25

def each(&block)
  output.each(&block)
end

#errors(options = EMPTY_HASH) ⇒ Object



45
46
47
# File 'lib/dry/validation/result.rb', line 45

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

#failure?Boolean

Returns:

  • (Boolean)


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

def failure?
  !success?
end

#hints(options = EMPTY_HASH) ⇒ Object



49
50
51
# File 'lib/dry/validation/result.rb', line 49

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

#message_set(options = EMPTY_HASH) ⇒ Object



53
54
55
# File 'lib/dry/validation/result.rb', line 53

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

#messages(options = EMPTY_HASH) ⇒ Object



41
42
43
# File 'lib/dry/validation/result.rb', line 41

def messages(options = EMPTY_HASH)
  message_set(options).dump
end

#nameObject



69
70
71
# File 'lib/dry/validation/result.rb', line 69

def name
  Array(path).last
end

#success?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/dry/validation/result.rb', line 33

def success?
  results.empty?
end

#to_astObject



57
58
59
60
61
62
63
# File 'lib/dry/validation/result.rb', line 57

def to_ast
  if name
    [type, [name, [:set, result_ast]]]
  else
    ast
  end
end

#to_either(options = EMPTY_HASH) ⇒ Object



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

def to_either(options = EMPTY_HASH)
  if success?
    Right(output)
  else
    Left(messages(options))
  end
end