Class: Dry::Validation::Result
- Inherits:
-
Object
- Object
- Dry::Validation::Result
- Includes:
- Enumerable
- Defined in:
- lib/dry/validation/result.rb
Constant Summary collapse
- EMPTY_MESSAGES =
{}.freeze
Instance Attribute Summary collapse
-
#error_compiler ⇒ Object
readonly
Returns the value of attribute error_compiler.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#hint_compiler ⇒ Object
readonly
Returns the value of attribute hint_compiler.
-
#output ⇒ Object
(also: #to_hash, #to_h)
readonly
Returns the value of attribute output.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #each(&block) ⇒ Object
- #failure? ⇒ Boolean
-
#initialize(output, errors, error_compiler, hint_compiler) ⇒ Result
constructor
A new instance of Result.
- #messages(options = {}) ⇒ Object
- #success? ⇒ Boolean
- #to_ast ⇒ Object
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_compiler ⇒ Object (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 |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
8 9 10 |
# File 'lib/dry/validation/result.rb', line 8 def errors @errors end |
#hint_compiler ⇒ Object (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 |
#output ⇒ Object (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
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 ||= begin return EMPTY_MESSAGES if success? hints = hint_compiler.with().call msg_set = error_compiler.with().(error_ast).with_hints!(hints) as_hash = .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
32 33 34 |
# File 'lib/dry/validation/result.rb', line 32 def success? errors.empty? end |
#to_ast ⇒ Object
58 59 60 |
# File 'lib/dry/validation/result.rb', line 58 def to_ast [:set, error_ast] end |