Class: Hubbado::Trailblazer::RunOperation::Result

Inherits:
Object
  • Object
show all
Includes:
Log::Dependency
Defined in:
lib/hubbado/trailblazer/run_operation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = policy_unauthorized_message
end

Instance Attribute Details

#policy_unauthorized_messageObject (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
  @policy_unauthorized_message
end

#returnedObject (readonly)

Returns the value of attribute returned.



121
122
123
# File 'lib/hubbado/trailblazer/run_operation.rb', line 121

def returned
  @returned
end

#trace_operationObject (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

Returns:

  • (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_levelObject



132
133
134
# File 'lib/hubbado/trailblazer/run_operation.rb', line 132

def log_level
  trace_operation ? :debug : :info
end

#not_foundObject



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

Returns:

  • (Boolean)


242
243
244
# File 'lib/hubbado/trailblazer/run_operation.rb', line 242

def not_found_executed?
  !!@not_found_executed
end

#otherwiseObject



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

Returns:

  • (Boolean)


246
247
248
# File 'lib/hubbado/trailblazer/run_operation.rb', line 246

def otherwise_executed?
  !!@otherwise_executed
end

#policy_failedObject



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

Returns:

  • (Boolean)


234
235
236
# File 'lib/hubbado/trailblazer/run_operation.rb', line 234

def policy_failed_executed?
  !!@policy_failed_executed
end

#raise_not_foundObject

Raises:

  • (ActiveRecord::RecordNotFound)


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_failedObject

Raises:

  • (StandardError)


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"

  error_messages = ctx['contract.default']&.errors&.full_messages

  if error_messages&.any?
    msg += " with errors:\n\n#{error_messages.map { |e| "  - #{e}" }.join("\n")}"
  end

  raise StandardError, msg
end

#raise_policy_failedObject



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(
    policy_unauthorized_message,
    ctx['result.policy.default'][:policy_result]
  )
end

#successObject



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

Returns:

  • (Boolean)


230
231
232
# File 'lib/hubbado/trailblazer/run_operation.rb', line 230

def success_executed?
  !!@success_executed
end

#validation_failedObject



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.full_messages.empty?

  @validation_failed_executed = true
  @returned = yield(ctx)

  if trace_operation
    logger.send(
      log_level,
      "Validation failed: #{ctx['contract.default'].errors.full_messages.join(', ')}"
    )
  else
    logger.send(log_level, "Validation failed")
  end
  @returned
end

#validation_failed_executed?Boolean

Returns:

  • (Boolean)


238
239
240
# File 'lib/hubbado/trailblazer/run_operation.rb', line 238

def validation_failed_executed?
  !!@validation_failed_executed
end