Class: ActiveInteractor::Result
- Inherits:
-
Object
- Object
- ActiveInteractor::Result
- Includes:
- ActiveModelErrorMethods
- Defined in:
- lib/active_interactor/result.rb
Overview
The object returned by an interactor
Constant Summary collapse
- STATUS =
{ success: 0, failed_at_input: 1, failed_at_runtime: 2, failed_at_output: 3 }.freeze
Instance Attribute Summary collapse
-
#data ⇒ ActiveInteractor::Context::Result
readonly
The data returned by the interactor.
-
#errors ⇒ ActiveModel::Errors
readonly
The errors returned by the interactor.
Class Method Summary collapse
- .failure(data: {}, errors: {}, status: STATUS[:failed_at_runtime]) ⇒ Object
- .success(data: {}) ⇒ Object
Instance Method Summary collapse
-
#failure? ⇒ Boolean
(also: #failed?)
Whether or not the result is a failure.
-
#initialize(status:, data: {}) ⇒ Result
constructor
A new instance of Result.
- #read_attribute_for_validation(attribute_name) ⇒ Object
-
#success? ⇒ Boolean
(also: #successful?)
Whether or not the result is a success.
-
#to_hash ⇒ Hash {Symbol => Boolean, Hash}
(also: #to_h)
The result as a Hash.
-
#to_json ⇒ Hash {Symbol => Boolean, Hash}
deprecated
Deprecated.
will be removed in version 2.0.0-alpha.3.0.0 use #to_hash instead
Constructor Details
#initialize(status:, data: {}) ⇒ Result
Returns a new instance of Result.
102 103 104 105 106 |
# File 'lib/active_interactor/result.rb', line 102 def initialize(status:, data: {}) @status = status @data = data @errors = ActiveModel::Errors.new(self) end |
Instance Attribute Details
#data ⇒ ActiveInteractor::Context::Result (readonly)
Returns the data returned by the interactor.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/active_interactor/result.rb', line 34 class Result include ActiveModelErrorMethods # @!method to_json # The {ActiveInteractor::Interactor::Base result} as a Hash # # @example When an {ActiveInteractor::Interactor::Base interactor} succeeds # result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'password') # result.to_hash # # #=> { :success => true, :errors => {}, :data => { :login => 'johndoe' } } # # @example When an {ActiveInteractor::Interactor::Base interactor} fails # result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'notpassword') # result.to_hash # # #=> { # #=> :success => false, # #=> :errors => { :password_confirmation => ["doesn't match Password"] }, # #=> :data => { :login => 'johndoe' } # #=> } # # @deprecated will be removed in version 2.0.0-alpha.3.0.0 # use {#to_hash} instead # @return [Hash {Symbol => Boolean, Hash}] delegate :to_json, to: :to_hash # @private STATUS = { success: 0, failed_at_input: 1, failed_at_runtime: 2, failed_at_output: 3 }.freeze attr_reader :data class << self # @private def success(data: {}) new(status: STATUS[:success], data: data) end # @private def failure(data: {}, errors: {}, status: STATUS[:failed_at_runtime]) result = new(status: status, data: data) parse_errors(errors).each_pair do |attribute, | Array.wrap().each { || result.errors.add(attribute, ) } end result end private def parse_errors(errors) case errors when String { generic: [errors] } when ActiveModel::Errors errors.to_hash else errors end end end private_class_method :new # @private def initialize(status:, data: {}) @status = status @data = data @errors = ActiveModel::Errors.new(self) end # Whether or not the {ActiveInteractor::Interactor::Base result} is a failure # # @example When an {ActiveInteractor::Interactor::Base interactor} fails # result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'notpassword') # result.failure? # # #=> true # # @example When an {ActiveInteractor::Interactor::Base interactor} succeeds # result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'password') # result.failure? # # #=> false # # @return [Boolean] def failure? !success? end alias failed? failure? # @private def read_attribute_for_validation(attribute_name) data&.send(attribute_name.to_sym) end # Whether or not the {ActiveInteractor::Interactor::Base result} is a success # # @return [Boolean] def success? @status == STATUS[:success] end alias successful? success? # The {ActiveInteractor::Interactor::Base result} as a Hash # # @example When an {ActiveInteractor::Interactor::Base interactor} succeeds # result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'password') # result.to_hash # # #=> { :success => true, :errors => {}, :data => { :login => 'johndoe' } } # # @example When an {ActiveInteractor::Interactor::Base interactor} fails # result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'notpassword') # result.to_hash # # #=> { # #=> :success => false, # #=> :errors => { :password_confirmation => ["doesn't match Password"] }, # #=> :data => { :login => 'johndoe' } # #=> } # # @return [Hash {Symbol => Boolean, Hash}] def to_hash { success: success?, errors: errors.to_hash, data: data.to_json } end alias to_h to_hash end |
#errors ⇒ ActiveModel::Errors (readonly)
Returns the errors returned by the interactor.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/active_interactor/result.rb', line 34 class Result include ActiveModelErrorMethods # @!method to_json # The {ActiveInteractor::Interactor::Base result} as a Hash # # @example When an {ActiveInteractor::Interactor::Base interactor} succeeds # result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'password') # result.to_hash # # #=> { :success => true, :errors => {}, :data => { :login => 'johndoe' } } # # @example When an {ActiveInteractor::Interactor::Base interactor} fails # result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'notpassword') # result.to_hash # # #=> { # #=> :success => false, # #=> :errors => { :password_confirmation => ["doesn't match Password"] }, # #=> :data => { :login => 'johndoe' } # #=> } # # @deprecated will be removed in version 2.0.0-alpha.3.0.0 # use {#to_hash} instead # @return [Hash {Symbol => Boolean, Hash}] delegate :to_json, to: :to_hash # @private STATUS = { success: 0, failed_at_input: 1, failed_at_runtime: 2, failed_at_output: 3 }.freeze attr_reader :data class << self # @private def success(data: {}) new(status: STATUS[:success], data: data) end # @private def failure(data: {}, errors: {}, status: STATUS[:failed_at_runtime]) result = new(status: status, data: data) parse_errors(errors).each_pair do |attribute, | Array.wrap().each { || result.errors.add(attribute, ) } end result end private def parse_errors(errors) case errors when String { generic: [errors] } when ActiveModel::Errors errors.to_hash else errors end end end private_class_method :new # @private def initialize(status:, data: {}) @status = status @data = data @errors = ActiveModel::Errors.new(self) end # Whether or not the {ActiveInteractor::Interactor::Base result} is a failure # # @example When an {ActiveInteractor::Interactor::Base interactor} fails # result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'notpassword') # result.failure? # # #=> true # # @example When an {ActiveInteractor::Interactor::Base interactor} succeeds # result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'password') # result.failure? # # #=> false # # @return [Boolean] def failure? !success? end alias failed? failure? # @private def read_attribute_for_validation(attribute_name) data&.send(attribute_name.to_sym) end # Whether or not the {ActiveInteractor::Interactor::Base result} is a success # # @return [Boolean] def success? @status == STATUS[:success] end alias successful? success? # The {ActiveInteractor::Interactor::Base result} as a Hash # # @example When an {ActiveInteractor::Interactor::Base interactor} succeeds # result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'password') # result.to_hash # # #=> { :success => true, :errors => {}, :data => { :login => 'johndoe' } } # # @example When an {ActiveInteractor::Interactor::Base interactor} fails # result = CreateUser.perform(login: 'johndoe', password: 'password', password_confirmation: 'notpassword') # result.to_hash # # #=> { # #=> :success => false, # #=> :errors => { :password_confirmation => ["doesn't match Password"] }, # #=> :data => { :login => 'johndoe' } # #=> } # # @return [Hash {Symbol => Boolean, Hash}] def to_hash { success: success?, errors: errors.to_hash, data: data.to_json } end alias to_h to_hash end |
Class Method Details
.failure(data: {}, errors: {}, status: STATUS[:failed_at_runtime]) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/active_interactor/result.rb', line 78 def failure(data: {}, errors: {}, status: STATUS[:failed_at_runtime]) result = new(status: status, data: data) parse_errors(errors).each_pair do |attribute, | Array.wrap().each { || result.errors.add(attribute, ) } end result end |
.success(data: {}) ⇒ Object
73 74 75 |
# File 'lib/active_interactor/result.rb', line 73 def success(data: {}) new(status: STATUS[:success], data: data) end |
Instance Method Details
#failure? ⇒ Boolean Also known as: failed?
Whether or not the result is a failure
123 124 125 |
# File 'lib/active_interactor/result.rb', line 123 def failure? !success? end |
#read_attribute_for_validation(attribute_name) ⇒ Object
129 130 131 |
# File 'lib/active_interactor/result.rb', line 129 def read_attribute_for_validation(attribute_name) data&.send(attribute_name.to_sym) end |
#success? ⇒ Boolean Also known as: successful?
Whether or not the result is a success
136 137 138 |
# File 'lib/active_interactor/result.rb', line 136 def success? @status == STATUS[:success] end |
#to_hash ⇒ Hash {Symbol => Boolean, Hash} Also known as: to_h
The result as a Hash
160 161 162 163 164 165 166 |
# File 'lib/active_interactor/result.rb', line 160 def to_hash { success: success?, errors: errors.to_hash, data: data.to_json } end |