Class: SenroUsecaser::Hook

Inherits:
Object
  • Object
show all
Extended by:
DependsOn
Defined in:
lib/senro_usecaser/hook.rb

Overview

Base class for hooks with dependency injection support

Hook classes provide a way to define before/after/around hooks with access to the DI container and automatic dependency resolution.

Examples:

Basic hook

class LoggingHook < SenroUsecaser::Hook
  depends_on :logger, Logger

  def before(input)
    logger.info("Starting with #{input.class.name}")
  end

  def after(input, result)
    logger.info("Finished: #{result.success? ? 'success' : 'failure'}")
  end
end

Hook with namespace

class Admin::AuditHook < SenroUsecaser::Hook
  namespace :admin
  depends_on :audit_logger, AuditLogger

  def after(input, result)
    audit_logger.log(input: input, result: result)
  end
end

Hook with around

class TransactionHook < SenroUsecaser::Hook
  def around(input)
    ActiveRecord::Base.transaction { yield }
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DependsOn

copy_depends_on_to, declared_namespace, dependencies, dependency_types, depends_on, extended, namespace

Constructor Details

#initialize(container:, use_case_namespace: nil) ⇒ Hook

Initializes the hook with dependencies resolved from the container

: (container: Container, ?use_case_namespace: (Symbol | String)?) -> void



59
60
61
62
63
64
65
# File 'lib/senro_usecaser/hook.rb', line 59

def initialize(container:, use_case_namespace: nil)
  @_container = container
  @_use_case_namespace = use_case_namespace
  @_dependencies = {} #: Hash[Symbol, untyped]

  resolve_dependencies
end

Class Method Details

.inherited(subclass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



50
51
52
53
# File 'lib/senro_usecaser/hook.rb', line 50

def inherited(subclass)
  super
  copy_depends_on_to(subclass)
end

Instance Method Details

#after(input, result) ⇒ Object

Called after the UseCase executes Override in subclass to add after logic

: (untyped, Result) -> void



79
80
81
# File 'lib/senro_usecaser/hook.rb', line 79

def after(input, result)
  # Override in subclass
end

#around(_input) ⇒ Object

Wraps the UseCase execution Override in subclass to add around logic

: (untyped) { () -> Result } -> Result



87
88
89
# File 'lib/senro_usecaser/hook.rb', line 87

def around(_input)
  yield
end

#before(input) ⇒ Object

Called before the UseCase executes Override in subclass to add before logic

: (untyped) -> void



71
72
73
# File 'lib/senro_usecaser/hook.rb', line 71

def before(input)
  # Override in subclass
end

#on_failure(input, result, context = nil) ⇒ Object

Called when the UseCase fails Override in subclass to add failure handling or rollback logic

: (untyped, Result, ?RetryContext?) -> void

Examples:

Basic logging

def on_failure(input, result)
  logger.error("Failed: #{result.errors.first&.message}")
end

Request retry

def on_failure(input, result, context)
  if result.errors.first&.code == :network_error && context.attempt < 3
    context.retry!(wait: 2.0)
  end
end


107
108
109
# File 'lib/senro_usecaser/hook.rb', line 107

def on_failure(input, result, context = nil)
  # Override in subclass
end