Class: Dry::Validation::Result

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

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
23
# 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
  @messages = EMPTY_HASH if success?
end

Instance Attribute Details

#error_compilerObject (readonly)

Returns the value of attribute error_compiler.



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

def error_compiler
  @error_compiler
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#hint_compilerObject (readonly)

Returns the value of attribute hint_compiler.



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

def hint_compiler
  @hint_compiler
end

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

Returns the value of attribute output.



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

def output
  @output
end

Instance Method Details

#[](name) ⇒ Object



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

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

#each(&block) ⇒ Object



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

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

#failure?Boolean

Returns:

  • (Boolean)


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

def failure?
  !success?
end

#message_set(options = EMPTY_HASH) ⇒ Object



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

def message_set(options = EMPTY_HASH)
  error_compiler
    .with(options).(error_ast)
    .with_hints!(hint_compiler.with(options).())
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

#success?Boolean

Returns:

  • (Boolean)


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

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