Class: Hubbado::Trailblazer::RunOperation::Result
- Inherits:
-
Object
- Object
- Hubbado::Trailblazer::RunOperation::Result
- Includes:
- Log::Dependency
- Defined in:
- lib/hubbado/trailblazer/run_operation.rb
Instance Attribute Summary collapse
-
#policy_unauthorized_message ⇒ Object
readonly
Returns the value of attribute policy_unauthorized_message.
-
#returned ⇒ Object
readonly
Returns the value of attribute returned.
-
#trace_operation ⇒ Object
readonly
Returns the value of attribute trace_operation.
Instance Method Summary collapse
- #executed? ⇒ Boolean
-
#initialize(operation, ctx, trace_operation:, policy_unauthorized_message:) ⇒ Result
constructor
A new instance of Result.
- #log_level ⇒ Object
- #not_found ⇒ Object
- #not_found_executed? ⇒ Boolean
- #otherwise ⇒ Object
- #otherwise_executed? ⇒ Boolean
- #policy_failed ⇒ Object
- #policy_failed_executed? ⇒ Boolean
- #raise_not_found ⇒ Object
- #raise_operation_failed ⇒ Object
- #raise_policy_failed ⇒ Object
- #success ⇒ Object
- #success_executed? ⇒ Boolean
- #validation_failed ⇒ Object
- #validation_failed_executed? ⇒ Boolean
Constructor Details
#initialize(operation, ctx, trace_operation:, policy_unauthorized_message:) ⇒ Result
Returns a new instance of Result.
125 126 127 128 129 130 |
# File 'lib/hubbado/trailblazer/run_operation.rb', line 125 def initialize(operation, ctx, trace_operation:, policy_unauthorized_message:) @operation = operation @ctx = ctx @trace_operation = trace_operation @policy_unauthorized_message = end |
Instance Attribute Details
#policy_unauthorized_message ⇒ Object (readonly)
Returns the value of attribute policy_unauthorized_message.
123 124 125 |
# File 'lib/hubbado/trailblazer/run_operation.rb', line 123 def @policy_unauthorized_message end |
#returned ⇒ Object (readonly)
Returns the value of attribute returned.
121 122 123 |
# File 'lib/hubbado/trailblazer/run_operation.rb', line 121 def returned @returned end |
#trace_operation ⇒ Object (readonly)
Returns the value of attribute trace_operation.
122 123 124 |
# File 'lib/hubbado/trailblazer/run_operation.rb', line 122 def trace_operation @trace_operation end |
Instance Method Details
#executed? ⇒ Boolean
250 251 252 253 254 255 256 |
# File 'lib/hubbado/trailblazer/run_operation.rb', line 250 def executed? success_executed? || policy_failed_executed? || validation_failed_executed? || not_found_executed? || otherwise_executed? end |
#log_level ⇒ Object
132 133 134 |
# File 'lib/hubbado/trailblazer/run_operation.rb', line 132 def log_level trace_operation ? :debug : :info end |
#not_found ⇒ Object
185 186 187 188 189 190 191 192 193 |
# File 'lib/hubbado/trailblazer/run_operation.rb', line 185 def not_found return unless ctx.terminus.to_h[:semantic] == :not_found @not_found_executed = true @returned = yield(ctx) logger.send(log_level, "Not found block executed for operation #{operation}") @returned end |
#not_found_executed? ⇒ Boolean
242 243 244 |
# File 'lib/hubbado/trailblazer/run_operation.rb', line 242 def not_found_executed? !!@not_found_executed end |
#otherwise ⇒ Object
195 196 197 198 199 200 201 202 203 |
# File 'lib/hubbado/trailblazer/run_operation.rb', line 195 def otherwise return if executed? @otherwise_executed = true @returned = yield(ctx) logger.send(log_level, "Otherwise block executed for operation #{operation}") @returned end |
#otherwise_executed? ⇒ Boolean
246 247 248 |
# File 'lib/hubbado/trailblazer/run_operation.rb', line 246 def otherwise_executed? !!@otherwise_executed end |
#policy_failed ⇒ Object
146 147 148 149 150 151 152 153 154 |
# File 'lib/hubbado/trailblazer/run_operation.rb', line 146 def policy_failed return unless ctx['result.policy.default']&.failure? @policy_failed_executed = true @returned = yield(ctx) logger.send(log_level, "Policy failed block executed for operation #{operation}") @returned end |
#policy_failed_executed? ⇒ Boolean
234 235 236 |
# File 'lib/hubbado/trailblazer/run_operation.rb', line 234 def policy_failed_executed? !!@policy_failed_executed end |
#raise_not_found ⇒ Object
205 206 207 208 209 |
# File 'lib/hubbado/trailblazer/run_operation.rb', line 205 def raise_not_found msg = "Record for operation #{operation.name} not found" raise ActiveRecord::RecordNotFound, msg end |
#raise_operation_failed ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/hubbado/trailblazer/run_operation.rb', line 211 def raise_operation_failed msg = "Operation #{operation.name} failed" = ctx['contract.default']&.errors&. if &.any? msg += " with errors:\n\n#{.map { |e| " - #{e}" }.join("\n")}" end raise StandardError, msg end |
#raise_policy_failed ⇒ Object
223 224 225 226 227 228 |
# File 'lib/hubbado/trailblazer/run_operation.rb', line 223 def raise_policy_failed raise Hubbado::Trailblazer::Errors::Unauthorized.new( , ctx['result.policy.default'][:policy_result] ) end |
#success ⇒ Object
136 137 138 139 140 141 142 143 144 |
# File 'lib/hubbado/trailblazer/run_operation.rb', line 136 def success return unless ctx.success? @success_executed = true @returned = yield(ctx) logger.send(log_level, "Success block executed for operation #{operation}") @returned end |
#success_executed? ⇒ Boolean
230 231 232 |
# File 'lib/hubbado/trailblazer/run_operation.rb', line 230 def success_executed? !!@success_executed end |
#validation_failed ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/hubbado/trailblazer/run_operation.rb', line 156 def validation_failed return if success_executed? contract = ctx['contract.default'] # TODO: We cannot call `contract.valid?` here, since the following # steps will have converted form errors from strings to symbols: # # contract = Companies::Controls::Contracts::InviteMember.example # contract.validate({name: 'Some name', email: '[email protected]', role: :staff) # contract.errors.add :email, :taken # contract.valid? # # This only happens if errors are added outside of the validation return if contract.nil? || contract.errors..empty? @validation_failed_executed = true @returned = yield(ctx) if trace_operation logger.send( log_level, "Validation failed: #{ctx['contract.default'].errors..join(', ')}" ) else logger.send(log_level, "Validation failed") end @returned end |
#validation_failed_executed? ⇒ Boolean
238 239 240 |
# File 'lib/hubbado/trailblazer/run_operation.rb', line 238 def validation_failed_executed? !!@validation_failed_executed end |