Class: Operations::Result
- Inherits:
-
Object
- Object
- Operations::Result
- Extended by:
- Dry::Initializer
- Defined in:
- lib/operations/result.rb
Overview
This is a the result of the operation. Considered a failure if contains any errors. Contains all the execution artifacts such as params and context (the initial one merged with the result of contract and operation routine execution). Also able to spawn a form object basing on the operation params and errors.
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
- #errors(**options) ⇒ Object
-
#failed_policy?(*error_codes) ⇒ Boolean
(also: #failed_policies?)
Checks if ANY of the passed policy codes have failed If nothing is passed - checks that ANY policy have failed.
-
#failed_precheck?(*error_codes) ⇒ Boolean
(also: #failed_prechecks?)
Checks if ANY of the passed precondition or policy codes have failed If nothing is passed - checks that ANY precondition or policy have failed.
-
#failed_precondition?(*error_codes) ⇒ Boolean
(also: #failed_preconditions?)
Checks if ANY of the passed precondition codes have failed If nothing is passed - checks that ANY precondition have failed.
- #failure? ⇒ Boolean
-
#form ⇒ Object
A form object that can be used for rendering forms with ‘form_for`, `simple_form` and other view helpers.
-
#merge(**changes) ⇒ Object
Instantiates a new result with the given fields updated.
- #success? ⇒ Boolean (also: #callable?)
- #to_hash(include_command: false) ⇒ Object
- #to_monad ⇒ Object
Instance Method Details
#as_json(options = {}) ⇒ Object
82 83 84 |
# File 'lib/operations/result.rb', line 82 def as_json( = {}) to_hash(**.slice(:include_command)) end |
#errors(**options) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/operations/result.rb', line 29 def errors(**) if @errors.respond_to?(:call) @errors.call(**) else .empty? ? @errors : @errors.with([], ).freeze end end |
#failed_policy?(*error_codes) ⇒ Boolean Also known as: failed_policies?
Checks if ANY of the passed policy codes have failed If nothing is passed - checks that ANY policy have failed
57 58 59 |
# File 'lib/operations/result.rb', line 57 def failed_policy?(*error_codes) component == :policies && failed_precheck?(*error_codes) end |
#failed_precheck?(*error_codes) ⇒ Boolean Also known as: failed_prechecks?
Checks if ANY of the passed precondition or policy codes have failed If nothing is passed - checks that ANY precondition or policy have failed
48 49 50 51 52 |
# File 'lib/operations/result.rb', line 48 def failed_precheck?(*error_codes) failure? && %i[policies preconditions].include?(component) && (error_codes.blank? || errors_with_code?(*error_codes)) end |
#failed_precondition?(*error_codes) ⇒ Boolean Also known as: failed_preconditions?
Checks if ANY of the passed precondition codes have failed If nothing is passed - checks that ANY precondition have failed
64 65 66 |
# File 'lib/operations/result.rb', line 64 def failed_precondition?(*error_codes) component == :preconditions && failed_precheck?(*error_codes) end |
#failure? ⇒ Boolean
42 43 44 |
# File 'lib/operations/result.rb', line 42 def failure? !success? end |
#form ⇒ Object
A form object that can be used for rendering forms with ‘form_for`, `simple_form` and other view helpers.
75 76 77 78 79 80 |
# File 'lib/operations/result.rb', line 75 def form @form ||= operation.form_class.new( operation.form_hydrator.call(operation.form_class, params, **context), messages: errors.to_h ) end |
#merge(**changes) ⇒ Object
Instantiates a new result with the given fields updated
25 26 27 |
# File 'lib/operations/result.rb', line 25 def merge(**changes) self.class.new(**self.class.dry_initializer.attributes(self), **changes) end |
#success? ⇒ Boolean Also known as: callable?
37 38 39 |
# File 'lib/operations/result.rb', line 37 def success? errors.empty? end |
#to_hash(include_command: false) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/operations/result.rb', line 86 def to_hash(include_command: false) hash = { component: component, params: params, context: context_to_hash, on_success: on_success.as_json, on_failure: on_failure.as_json, errors: errors(full: true).to_h } hash[:command] = operation&.to_hash if include_command hash end |
#to_monad ⇒ Object
69 70 71 |
# File 'lib/operations/result.rb', line 69 def to_monad success? ? Success(self) : Failure(self) end |