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.



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

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, to_h

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



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

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

#each(&block) ⇒ Object



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

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

#failure?Boolean

Returns:

  • (Boolean)


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

def failure?
  !success?
end

#messages(options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dry/validation/result.rb', line 40

def messages(options = {})
  @messages ||=
    begin
      return EMPTY_MESSAGES if success?
      hints = hint_compiler.with(options).call
      msg_set = error_compiler.with(options).(error_ast).with_hints!(hints)

      as_hash = options.fetch(:as_hash, true)

      if as_hash
        hash = msg_set.to_h
        hash.key?(nil) ? hash.values.flatten : hash
      else
        msg_set
      end
    end
end

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  errors.empty?
end

#to_astObject



58
59
60
# File 'lib/dry/validation/result.rb', line 58

def to_ast
  [:set, error_ast]
end