Class: Dry::Validation::Result

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dry/validation/result.rb

Constant Summary collapse

EMPTY_MESSAGES =
{}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, errors, error_compiler, hint_compiler) ⇒ Result

Returns a new instance of Result.



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

def initialize(output, errors, error_compiler, hint_compiler)
  @output = output
  @errors = errors
  @error_compiler = error_compiler
  @hint_compiler = hint_compiler
end

Instance Attribute Details

#error_compilerObject (readonly)

Returns the value of attribute error_compiler.



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

def error_compiler
  @error_compiler
end

#errorsObject (readonly)

Returns the value of attribute errors.



8
9
10
# File 'lib/dry/validation/result.rb', line 8

def errors
  @errors
end

#hint_compilerObject (readonly)

Returns the value of attribute hint_compiler.



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

def hint_compiler
  @hint_compiler
end

#outputObject (readonly) Also known as: to_hash

Returns the value of attribute output.



7
8
9
# File 'lib/dry/validation/result.rb', line 7

def output
  @output
end

Instance Method Details

#[](name) ⇒ Object



27
28
29
# File 'lib/dry/validation/result.rb', line 27

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

#each(&block) ⇒ Object



23
24
25
# File 'lib/dry/validation/result.rb', line 23

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

#failure?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/dry/validation/result.rb', line 35

def failure?
  !success?
end

#messages(options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dry/validation/result.rb', line 39

def messages(options = {})
  @messages ||=
    begin
      return EMPTY_MESSAGES if success?

      hints = hint_compiler.with(options).call
      comp = error_compiler.with(options.merge(hints: hints))

      comp.(error_ast)
    end
end

#success?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/dry/validation/result.rb', line 31

def success?
  errors.empty?
end

#to_astObject



51
52
53
# File 'lib/dry/validation/result.rb', line 51

def to_ast
  [:set, error_ast]
end