Class: CommandTower::ServiceBase

Inherits:
Object
  • Object
show all
Includes:
ArgumentValidation, Execution::ContextAccess, Logging::LifecycleDeclaration, ServiceLogging, Interactor
Defined in:
app/services/command_tower/service_base.rb

Defined Under Namespace

Classes: ArgumentValidationError, CompositionValidationError, ConfigurationError, DefaultValueError, KeyValidationError, NameConflictError, NestedOneOfError, OneOfError, ServiceBaseError, ValidationError

Constant Summary collapse

ON_ARGUMENT_VALIDATION =
[
  DEFAULT_VALIDATION = :raise,
  :fail_early,
  :log,
]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging::LifecycleDeclaration

included

Methods included from Execution::ContextAccess

#audit, #execution_context, #log_debug, #log_error, #log_info, #log_warn, #publish_event

Methods included from ArgumentValidation

included

Methods included from ServiceLogging

included

Class Method Details

.inherited(subclass) ⇒ Object



31
32
33
34
35
36
# File 'app/services/command_tower/service_base.rb', line 31

def self.inherited(subclass)
  subclass.around(:internal_validate)
  subclass.after(:sanitize_params)
  # Registered last so Interactor treats it as the outermost around (reverse nest).
  subclass.around(:command_tower_lifecycle)
end

Instance Method Details

#command_tower_lifecycle(interactor) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/services/command_tower/service_base.rb', line 48

def command_tower_lifecycle(interactor)
  CommandTower::Events.around_execution(layer: :service, subject: self.class.name, log_lifecycle: self.class.lifecycle_loggable?) do |record|
    begin
      interactor.call
      record[:result] = :success
    rescue ::Interactor::Failure
      record[:result] = :failure
      record[:error_codes] = service_lifecycle_error_codes
      record[:log_level] = service_lifecycle_log_level
      raise
    rescue ::Exception => e
      record[:unexpected] = e
      raise
    end
  end
end

#internal_validate(interactor) ⇒ Object



42
43
44
45
46
# File 'app/services/command_tower/service_base.rb', line 42

def internal_validate(interactor)
  validate! # custom validations defined on the child class
  run_validations! # ArgumentValidation's based on defined settings on child
  interactor.call
end

#service_lifecycle_error_codesObject



65
66
67
68
69
70
# File 'app/services/command_tower/service_base.rb', line 65

def service_lifecycle_error_codes
  error = context.application_error if context.respond_to?(:application_error)
  return [error.code] if error.respond_to?(:code)

  nil
end

#service_lifecycle_log_levelObject



72
73
74
75
# File 'app/services/command_tower/service_base.rb', line 72

def service_lifecycle_log_level
  error = context.application_error if context.respond_to?(:application_error)
  error.log_level if error.respond_to?(:log_level)
end

#validate!Object



38
39
40
# File 'app/services/command_tower/service_base.rb', line 38

def validate!
  # overload from child
end