Class: Dry::Validation::Result
- Inherits:
-
Object
- Object
- Dry::Validation::Result
- Includes:
- Monads::Result::Mixin, Enumerable
- Defined in:
- lib/dry/validation/result.rb,
lib/dry/validation/extensions/monads.rb
Instance Attribute Summary collapse
-
#message_compiler ⇒ Object
readonly
Returns the value of attribute message_compiler.
-
#output ⇒ Object
(also: #to_hash, #to_h)
readonly
Returns the value of attribute output.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #ast ⇒ Object
- #each(&block) ⇒ Object
- #errors(options = EMPTY_HASH) ⇒ Object
- #failure? ⇒ Boolean
- #hints(options = EMPTY_HASH) ⇒ Object
-
#initialize(output, results, message_compiler, path) ⇒ Result
constructor
A new instance of Result.
- #message_set(options = EMPTY_HASH) ⇒ Object
- #messages(options = EMPTY_HASH) ⇒ Object
- #name ⇒ Object
- #success? ⇒ Boolean
- #to_ast ⇒ Object
- #to_monad(options = EMPTY_HASH) ⇒ Object (also: #to_either)
Constructor Details
#initialize(output, results, message_compiler, path) ⇒ Result
Returns a new instance of Result.
17 18 19 20 21 22 |
# File 'lib/dry/validation/result.rb', line 17 def initialize(output, results, , path) @output = output @results = results @message_compiler = @path = path end |
Instance Attribute Details
#message_compiler ⇒ Object (readonly)
Returns the value of attribute message_compiler.
11 12 13 |
# File 'lib/dry/validation/result.rb', line 11 def @message_compiler end |
#output ⇒ Object (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 |
#path ⇒ Object (readonly)
Returns the value of attribute path.
12 13 14 |
# File 'lib/dry/validation/result.rb', line 12 def path @path end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
10 11 12 |
# File 'lib/dry/validation/result.rb', line 10 def results @results end |
Instance Method Details
#[](name) ⇒ Object
28 29 30 |
# File 'lib/dry/validation/result.rb', line 28 def [](name) output.fetch(name) end |
#ast ⇒ Object
64 65 66 |
# File 'lib/dry/validation/result.rb', line 64 def ast(*) [:set, result_ast] end |
#each(&block) ⇒ Object
24 25 26 |
# File 'lib/dry/validation/result.rb', line 24 def each(&block) output.each(&block) end |
#errors(options = EMPTY_HASH) ⇒ Object
44 45 46 |
# File 'lib/dry/validation/result.rb', line 44 def errors( = EMPTY_HASH) (.merge(hints: false)).dump end |
#failure? ⇒ Boolean
36 37 38 |
# File 'lib/dry/validation/result.rb', line 36 def failure? !success? end |
#hints(options = EMPTY_HASH) ⇒ Object
48 49 50 |
# File 'lib/dry/validation/result.rb', line 48 def hints( = EMPTY_HASH) (.merge(failures: false)).dump end |
#message_set(options = EMPTY_HASH) ⇒ Object
52 53 54 |
# File 'lib/dry/validation/result.rb', line 52 def ( = EMPTY_HASH) .with().(result_ast) end |
#messages(options = EMPTY_HASH) ⇒ Object
40 41 42 |
# File 'lib/dry/validation/result.rb', line 40 def ( = EMPTY_HASH) ().dump end |
#name ⇒ Object
68 69 70 |
# File 'lib/dry/validation/result.rb', line 68 def name Array(path).last end |
#success? ⇒ Boolean
32 33 34 |
# File 'lib/dry/validation/result.rb', line 32 def success? results.empty? end |
#to_ast ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/dry/validation/result.rb', line 56 def to_ast if name [type, [name, [:set, result_ast]]] else ast end end |
#to_monad(options = EMPTY_HASH) ⇒ Object Also known as: to_either
8 9 10 11 12 13 14 |
# File 'lib/dry/validation/extensions/monads.rb', line 8 def to_monad( = EMPTY_HASH) if success? Success(output) else Failure(()) end end |