Class: Servus::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/servus/config.rb

Overview

Configuration settings for the Servus gem.

Manages global configuration options including schema file locations. Access the configuration via config or modify via configure.

Examples:

Customizing schema location

Servus.configure do |config|
  config.schema_root = Rails.root.join('lib/schemas')
end

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

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.

Initializes a new configuration with default values.



122
123
124
125
126
127
128
129
130
# File 'lib/servus/config.rb', line 122

def initialize
  set_default_directories
  @strict_event_validation          = true
  @include_default_guards           = true
  @lockdown_enabled                 = true
  @require_service_arguments_schema = false
  @require_service_result_schema    = false
  @require_event_payload_schema     = false
end

Instance Attribute Details

#events_dirString

The directory where event handlers are located.

Defaults to ‘Rails.root/app/events` in Rails applications.

Returns:

  • (String)

    the events directory path



30
31
32
# File 'lib/servus/config.rb', line 30

def events_dir
  @events_dir
end

#guards_dirString

The directory where guard classes are located.

Defaults to ‘Rails.root/app/guards` in Rails applications.

Returns:

  • (String)

    the guards directory path



52
53
54
# File 'lib/servus/config.rb', line 52

def guards_dir
  @guards_dir
end

#include_default_guardsBoolean

Whether to include the default built-in guards (EnsurePresent, EnsurePositive).

Returns:

  • (Boolean)

    true to include default guards, false to exclude them



66
67
68
# File 'lib/servus/config.rb', line 66

def include_default_guards
  @include_default_guards
end

#lockdown_enabledBoolean

Whether external instantiation of services is blocked and instance ‘#call` methods are automatically privatized.

When enabled (default), callers must invoke services via the class method Base.call, which runs argument validation, logging, benchmarking, guards, result validation, and event emission. Calling ‘MyService.new` or `instance.call` directly raises `NoMethodError`.

Disable this if you have existing code that instantiates services directly or otherwise prefer to opt out of the enforcement.

Returns:

  • (Boolean)

    true to enforce lockdown (default), false to allow direct instantiation and public instance ‘#call`

See Also:



107
108
109
# File 'lib/servus/config.rb', line 107

def lockdown_enabled
  @lockdown_enabled
end

#require_event_payload_schemaBoolean

Whether to require all event handlers to define a payload schema.

When enabled, raises Support::Errors::SchemaRequiredError when an event handler validates a payload without a payload schema defined.

Returns:

  • (Boolean)

    true to require payload schemas, false to allow schema-less handlers



91
92
93
# File 'lib/servus/config.rb', line 91

def require_event_payload_schema
  @require_event_payload_schema
end

#require_service_arguments_schemaBoolean

Whether to require all services to define an arguments schema.

When enabled, raises Support::Errors::SchemaRequiredError when a service is called without an arguments schema defined.

Returns:

  • (Boolean)

    true to require arguments schemas, false to allow schema-less services



74
75
76
# File 'lib/servus/config.rb', line 74

def require_service_arguments_schema
  @require_service_arguments_schema
end

#require_service_result_schemaBoolean

Whether to require all services to define a result schema.

When enabled, raises Support::Errors::SchemaRequiredError when a service returns a successful response without a result schema defined. Failure schemas remain optional regardless of this setting.

Returns:

  • (Boolean)

    true to require result schemas, false to allow schema-less services



83
84
85
# File 'lib/servus/config.rb', line 83

def require_service_result_schema
  @require_service_result_schema
end

#schemas_dirString

The directory where JSON schema files are located.

Defaults to ‘Rails.root/app/schemas/services` in Rails applications.

Returns:

  • (String)

    the schemas directory path



23
24
25
# File 'lib/servus/config.rb', line 23

def schemas_dir
  @schemas_dir
end

#services_dirString

The directory where services are located.

Defaults to ‘Rails.root/app/services` in Rails applications.

Returns:

  • (String)

    the services directory path



37
38
39
# File 'lib/servus/config.rb', line 37

def services_dir
  @services_dir
end

#strict_event_validationBoolean

Whether to validate that all event handlers subscribe to events that are actually emitted by services.

When enabled, raises an error on boot if handlers subscribe to non-existent events. Helps catch typos and orphaned handlers.

Returns:

  • (Boolean)

    true to validate, false to skip validation



45
46
47
# File 'lib/servus/config.rb', line 45

def strict_event_validation
  @strict_event_validation
end

#tests_dirString

The directory where generated spec/test files are placed.

Defaults to ‘“spec”`. Projects using Minitest or a custom test layout can override this (e.g., `“test”`) so generators write files into the correct location.

Returns:

  • (String)

    the tests directory path



61
62
63
# File 'lib/servus/config.rb', line 61

def tests_dir
  @tests_dir
end

Instance Method Details

#schema_dir_for(service_namespace) ⇒ String

Returns the directory containing a service’s schema files.

Examples:

config.schema_dir_for("process_payment")
# => "/full/path/app/schemas/process_payment"

Parameters:

  • service_namespace (String)

    underscored service namespace

Returns:

  • (String)

    directory path for the service’s schemas



161
162
163
# File 'lib/servus/config.rb', line 161

def schema_dir_for(service_namespace)
  File.join(root_path, schemas_dir, service_namespace)
end

#schema_path_for(service_namespace, type) ⇒ String

Returns the full path to a service’s schema file.

Examples:

config.schema_path_for("process_payment", "arguments")
# => "/full/path/app/schemas/process_payment/arguments.json"

Parameters:

  • service_namespace (String)

    underscored service namespace (e.g., “process_payment”)

  • type (String)

    schema type (“arguments” or “result”)

Returns:

  • (String)

    full path to the schema JSON file



149
150
151
# File 'lib/servus/config.rb', line 149

def schema_path_for(service_namespace, type)
  File.join(root_path, schemas_dir, service_namespace, "#{type}.json")
end

#set_default_directoriesObject



132
133
134
135
136
137
138
# File 'lib/servus/config.rb', line 132

def set_default_directories
  @guards_dir   = 'app/guards'
  @events_dir   = 'app/events'
  @schemas_dir  = 'app/schemas'
  @services_dir = 'app/services'
  @tests_dir    = 'spec'
end