servus 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.claude/commands/check-docs.md +1 -0
- data/.claude/commands/consistency-check.md +1 -0
- data/.claude/commands/fine-tooth-comb.md +1 -0
- data/.claude/commands/red-green-refactor.md +5 -0
- data/.claude/settings.json +15 -0
- data/.rubocop.yml +18 -2
- data/.yardopts +6 -0
- data/CHANGELOG.md +47 -0
- data/CLAUDE.md +10 -0
- data/IDEAS.md +5 -0
- data/READme.md +300 -47
- data/Rakefile +33 -0
- data/builds/servus-0.1.3.gem +0 -0
- data/builds/servus-0.1.4.gem +0 -0
- data/builds/servus-0.1.5.gem +0 -0
- data/docs/core/1_overview.md +77 -0
- data/docs/core/2_architecture.md +120 -0
- data/docs/core/3_service_objects.md +121 -0
- data/docs/current_focus.md +569 -0
- data/docs/features/1_schema_validation.md +119 -0
- data/docs/features/2_error_handling.md +121 -0
- data/docs/features/3_async_execution.md +81 -0
- data/docs/features/4_logging.md +64 -0
- data/docs/features/5_event_bus.md +244 -0
- data/docs/guides/1_common_patterns.md +90 -0
- data/docs/guides/2_migration_guide.md +175 -0
- data/docs/integration/1_configuration.md +104 -0
- data/docs/integration/2_testing.md +287 -0
- data/docs/integration/3_rails_integration.md +99 -0
- data/docs/yard/Servus/Base.html +1645 -0
- data/docs/yard/Servus/Config.html +582 -0
- data/docs/yard/Servus/Extensions/Async/Call.html +400 -0
- data/docs/yard/Servus/Extensions/Async/Errors/AsyncError.html +140 -0
- data/docs/yard/Servus/Extensions/Async/Errors/JobEnqueueError.html +154 -0
- data/docs/yard/Servus/Extensions/Async/Errors/ServiceNotFoundError.html +154 -0
- data/docs/yard/Servus/Extensions/Async/Errors.html +128 -0
- data/docs/yard/Servus/Extensions/Async/Ext.html +119 -0
- data/docs/yard/Servus/Extensions/Async/Job.html +310 -0
- data/docs/yard/Servus/Extensions/Async.html +141 -0
- data/docs/yard/Servus/Extensions.html +117 -0
- data/docs/yard/Servus/Generators/ServiceGenerator.html +261 -0
- data/docs/yard/Servus/Generators.html +115 -0
- data/docs/yard/Servus/Helpers/ControllerHelpers.html +457 -0
- data/docs/yard/Servus/Helpers.html +115 -0
- data/docs/yard/Servus/Railtie.html +134 -0
- data/docs/yard/Servus/Support/Errors/AuthenticationError.html +287 -0
- data/docs/yard/Servus/Support/Errors/BadRequestError.html +283 -0
- data/docs/yard/Servus/Support/Errors/ForbiddenError.html +284 -0
- data/docs/yard/Servus/Support/Errors/InternalServerError.html +283 -0
- data/docs/yard/Servus/Support/Errors/NotFoundError.html +284 -0
- data/docs/yard/Servus/Support/Errors/ServiceError.html +489 -0
- data/docs/yard/Servus/Support/Errors/ServiceUnavailableError.html +290 -0
- data/docs/yard/Servus/Support/Errors/UnauthorizedError.html +200 -0
- data/docs/yard/Servus/Support/Errors/UnprocessableEntityError.html +288 -0
- data/docs/yard/Servus/Support/Errors/ValidationError.html +200 -0
- data/docs/yard/Servus/Support/Errors.html +140 -0
- data/docs/yard/Servus/Support/Logger.html +856 -0
- data/docs/yard/Servus/Support/Rescuer/BlockContext.html +585 -0
- data/docs/yard/Servus/Support/Rescuer/CallOverride.html +257 -0
- data/docs/yard/Servus/Support/Rescuer/ClassMethods.html +343 -0
- data/docs/yard/Servus/Support/Rescuer.html +267 -0
- data/docs/yard/Servus/Support/Response.html +574 -0
- data/docs/yard/Servus/Support/Validator.html +1150 -0
- data/docs/yard/Servus/Support.html +119 -0
- data/docs/yard/Servus/Testing/ExampleBuilders.html +523 -0
- data/docs/yard/Servus/Testing/ExampleExtractor.html +578 -0
- data/docs/yard/Servus/Testing.html +142 -0
- data/docs/yard/Servus.html +343 -0
- data/docs/yard/_index.html +535 -0
- data/docs/yard/class_list.html +54 -0
- data/docs/yard/css/common.css +1 -0
- data/docs/yard/css/full_list.css +58 -0
- data/docs/yard/css/style.css +503 -0
- data/docs/yard/file.1_common_patterns.html +154 -0
- data/docs/yard/file.1_configuration.html +115 -0
- data/docs/yard/file.1_overview.html +142 -0
- data/docs/yard/file.1_schema_validation.html +188 -0
- data/docs/yard/file.2_architecture.html +157 -0
- data/docs/yard/file.2_error_handling.html +190 -0
- data/docs/yard/file.2_migration_guide.html +242 -0
- data/docs/yard/file.2_testing.html +227 -0
- data/docs/yard/file.3_async_execution.html +145 -0
- data/docs/yard/file.3_rails_integration.html +160 -0
- data/docs/yard/file.3_service_objects.html +191 -0
- data/docs/yard/file.4_logging.html +135 -0
- data/docs/yard/file.ErrorHandling.html +190 -0
- data/docs/yard/file.READme.html +674 -0
- data/docs/yard/file.architecture.html +157 -0
- data/docs/yard/file.async_execution.html +145 -0
- data/docs/yard/file.common_patterns.html +154 -0
- data/docs/yard/file.configuration.html +115 -0
- data/docs/yard/file.error_handling.html +190 -0
- data/docs/yard/file.logging.html +135 -0
- data/docs/yard/file.migration_guide.html +242 -0
- data/docs/yard/file.overview.html +142 -0
- data/docs/yard/file.rails_integration.html +160 -0
- data/docs/yard/file.schema_validation.html +188 -0
- data/docs/yard/file.service_objects.html +191 -0
- data/docs/yard/file.testing.html +227 -0
- data/docs/yard/file_list.html +119 -0
- data/docs/yard/frames.html +22 -0
- data/docs/yard/index.html +674 -0
- data/docs/yard/js/app.js +344 -0
- data/docs/yard/js/full_list.js +242 -0
- data/docs/yard/js/jquery.js +4 -0
- data/docs/yard/method_list.html +542 -0
- data/docs/yard/top-level-namespace.html +110 -0
- data/lib/generators/servus/event_handler/event_handler_generator.rb +59 -0
- data/lib/generators/servus/event_handler/templates/handler.rb.erb +86 -0
- data/lib/generators/servus/event_handler/templates/handler_spec.rb.erb +48 -0
- data/lib/generators/servus/service/service_generator.rb +68 -1
- data/lib/generators/servus/service/templates/arguments.json.erb +19 -10
- data/lib/generators/servus/service/templates/result.json.erb +8 -2
- data/lib/generators/servus/service/templates/service.rb.erb +102 -5
- data/lib/generators/servus/service/templates/service_spec.rb.erb +67 -6
- data/lib/servus/base.rb +275 -58
- data/lib/servus/config.rb +83 -17
- data/lib/servus/event_handler.rb +275 -0
- data/lib/servus/events/bus.rb +137 -0
- data/lib/servus/events/emitter.rb +162 -0
- data/lib/servus/events/errors.rb +10 -0
- data/lib/servus/extensions/async/call.rb +50 -18
- data/lib/servus/extensions/async/errors.rb +23 -3
- data/lib/servus/extensions/async/ext.rb +10 -2
- data/lib/servus/extensions/async/job.rb +30 -9
- data/lib/servus/helpers/controller_helpers.rb +73 -37
- data/lib/servus/railtie.rb +16 -0
- data/lib/servus/support/errors.rb +135 -45
- data/lib/servus/support/rescuer.rb +189 -36
- data/lib/servus/support/response.rb +49 -7
- data/lib/servus/support/validator.rb +147 -19
- data/lib/servus/testing/example_builders.rb +133 -0
- data/lib/servus/testing/example_extractor.rb +309 -0
- data/lib/servus/testing/matchers.rb +88 -0
- data/lib/servus/testing.rb +19 -0
- data/lib/servus/version.rb +1 -1
- data/lib/servus.rb +6 -0
- metadata +135 -19
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# @title Core / 1. Overview
|
|
2
|
+
|
|
3
|
+
# Servus Overview
|
|
4
|
+
|
|
5
|
+
Servus is a lightweight framework for implementing service objects in Ruby applications. It extracts business logic from controllers and models into testable, single-purpose classes with built-in validation, error handling, and logging.
|
|
6
|
+
|
|
7
|
+
## Core Concepts
|
|
8
|
+
|
|
9
|
+
### The Service Pattern
|
|
10
|
+
|
|
11
|
+
Services encapsulate one business operation. Each service inherits from `Servus::Base`, implements `initialize` and `call`, and returns a `Response` object indicating success or failure.
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
class ProcessPayment::Service < Servus::Base
|
|
15
|
+
def initialize(user_id:, amount:)
|
|
16
|
+
@user_id = user_id
|
|
17
|
+
@amount = amount
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def call
|
|
21
|
+
user = User.find(@user_id)
|
|
22
|
+
return failure("Insufficient funds") unless user.balance >= @amount
|
|
23
|
+
|
|
24
|
+
user.update!(balance: user.balance - @amount)
|
|
25
|
+
success(user: user, new_balance: user.balance)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Usage
|
|
30
|
+
result = ProcessPayment::Service.call(user_id: 1, amount: 50)
|
|
31
|
+
result.success? # => true
|
|
32
|
+
result.data # => { user: #<User>, new_balance: 950 }
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Response Objects
|
|
36
|
+
|
|
37
|
+
Services return `Response` objects instead of raising exceptions for business failures. This makes success and failure paths explicit and enables service composition without exception handling.
|
|
38
|
+
|
|
39
|
+
```ruby
|
|
40
|
+
result = SomeService.call(params)
|
|
41
|
+
if result.success?
|
|
42
|
+
result.data # Hash or object returned by success()
|
|
43
|
+
else
|
|
44
|
+
result.error # ServiceError instance
|
|
45
|
+
result.error.message
|
|
46
|
+
result.error.api_error # { code: :symbol, message: "string" }
|
|
47
|
+
end
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Optional Schema Validation
|
|
51
|
+
|
|
52
|
+
Services can define JSON schemas for arguments and results. Validation happens automatically before/after execution but is entirely optional.
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
class Service < Servus::Base
|
|
56
|
+
schema(
|
|
57
|
+
arguments: {
|
|
58
|
+
type: "object",
|
|
59
|
+
required: ["user_id", "amount"],
|
|
60
|
+
properties: {
|
|
61
|
+
user_id: { type: "integer" },
|
|
62
|
+
amount: { type: "number", minimum: 0.01 }
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
)
|
|
66
|
+
end
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## When to Use Servus
|
|
70
|
+
|
|
71
|
+
**Good fits**: Multi-step workflows, operations spanning multiple models, external API calls, background jobs, complex business logic.
|
|
72
|
+
|
|
73
|
+
**Poor fits**: Simple CRUD, single-model operations, operations tightly coupled to one model.
|
|
74
|
+
|
|
75
|
+
## Framework Integration
|
|
76
|
+
|
|
77
|
+
Servus core works in any Ruby application. Rails-specific features (async via ActiveJob, controller helpers, generators) are optional additions. Services work without any configuration - just inherit from `Servus::Base` and implement your logic.
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# @title Core / 2. Architecture
|
|
2
|
+
|
|
3
|
+
# Architecture
|
|
4
|
+
|
|
5
|
+
Servus wraps service execution with automatic validation, logging, and error handling. When you call `Service.call(**args)`, the framework orchestrates these concerns transparently.
|
|
6
|
+
|
|
7
|
+
## Execution Flow
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
Arguments → Validation → Service#call → Result Validation → Event Emission → Logging → Response
|
|
11
|
+
↓ ↓ ↓ ↓
|
|
12
|
+
ValidationError ValidationError EventHandlers Benchmark
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
The framework intercepts the `.call` class method to inject cross-cutting concerns before and after your business logic runs. Your `call` instance method contains only business logic - validation, logging, event emission, and timing happen automatically.
|
|
16
|
+
|
|
17
|
+
## Core Components
|
|
18
|
+
|
|
19
|
+
**Servus::Base** (`lib/servus/base.rb`): Foundation class providing `.call()` orchestration and response helpers (`success`, `failure`, `error!`)
|
|
20
|
+
|
|
21
|
+
**Support::Response** (`lib/servus/support/response.rb`): Immutable result object with `success?`, `data`, and `error` attributes
|
|
22
|
+
|
|
23
|
+
**Support::Validator** (`lib/servus/support/validator.rb`): JSON Schema validation for arguments (before execution) and results (after execution). Schemas are cached after first load.
|
|
24
|
+
|
|
25
|
+
**Support::Logger** (`lib/servus/support/logger.rb`): Automatic logging at DEBUG (calls with args), INFO (success), WARN (failures), ERROR (exceptions)
|
|
26
|
+
|
|
27
|
+
**Support::Rescuer** (`lib/servus/support/rescuer.rb`): Declarative exception handling via `rescue_from` class method
|
|
28
|
+
|
|
29
|
+
**Support::Errors** (`lib/servus/support/errors.rb`): HTTP-aligned error hierarchy (ServiceError, NotFoundError, ValidationError, etc.)
|
|
30
|
+
|
|
31
|
+
**Events::Emitter** (`lib/servus/events/emitter.rb`): DSL for declaring events that services emit on success/failure
|
|
32
|
+
|
|
33
|
+
**Events::Bus** (`lib/servus/events/bus.rb`): Central event router using ActiveSupport::Notifications for thread-safe dispatch
|
|
34
|
+
|
|
35
|
+
**EventHandler** (`lib/servus/event_handler.rb`): Base class for handlers that subscribe to events and invoke services
|
|
36
|
+
|
|
37
|
+
## Extension Points
|
|
38
|
+
|
|
39
|
+
### Schema Validation
|
|
40
|
+
|
|
41
|
+
Use the `schema` DSL method to define JSON Schema validation for arguments and results:
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
44
|
+
class Service < Servus::Base
|
|
45
|
+
schema(
|
|
46
|
+
arguments: { type: "object", required: ["user_id"] },
|
|
47
|
+
result: { type: "object", required: ["user"] }
|
|
48
|
+
)
|
|
49
|
+
end
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Declarative Error Handling
|
|
53
|
+
|
|
54
|
+
Use `rescue_from` to convert exceptions into failures. Provide a custom error type or use a block for custom handling.
|
|
55
|
+
|
|
56
|
+
```ruby
|
|
57
|
+
class Service < Servus::Base
|
|
58
|
+
# Default error type
|
|
59
|
+
rescue_from Net::HTTPError, Timeout::Error, use: ServiceUnavailableError
|
|
60
|
+
|
|
61
|
+
# Custom handling with block
|
|
62
|
+
rescue_from ActiveRecord::RecordInvalid do |exception|
|
|
63
|
+
failure("Validation failed: #{exception.message}", type: ValidationError)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Support Classes
|
|
69
|
+
|
|
70
|
+
Create helper classes in `app/services/service_name/support/*.rb`. These are namespaced to your service.
|
|
71
|
+
|
|
72
|
+
```
|
|
73
|
+
app/services/process_payment/
|
|
74
|
+
├── service.rb
|
|
75
|
+
└── support/
|
|
76
|
+
├── payment_gateway.rb
|
|
77
|
+
└── receipt_formatter.rb
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Async Execution
|
|
81
|
+
|
|
82
|
+
`Service.call_async(**args)` enqueues execution via ActiveJob. The service runs identically whether called sync or async.
|
|
83
|
+
|
|
84
|
+
```ruby
|
|
85
|
+
ProcessPayment::Service.call_async(
|
|
86
|
+
user_id: 1,
|
|
87
|
+
amount: 50,
|
|
88
|
+
queue: :critical,
|
|
89
|
+
wait: 5.minutes
|
|
90
|
+
)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Event-Driven Architecture
|
|
94
|
+
|
|
95
|
+
Services can emit events that trigger downstream handlers. This decouples services from their side effects.
|
|
96
|
+
|
|
97
|
+
```ruby
|
|
98
|
+
# Service emits events
|
|
99
|
+
class CreateUser::Service < Servus::Base
|
|
100
|
+
emits :user_created, on: :success
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Handler reacts to events
|
|
104
|
+
class UserCreatedHandler < Servus::EventHandler
|
|
105
|
+
handles :user_created
|
|
106
|
+
|
|
107
|
+
invoke SendWelcomeEmail::Service, async: true do |payload|
|
|
108
|
+
{ user_id: payload[:user_id] }
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
See {file:docs/features/5_event_bus.md Event Bus} for full documentation.
|
|
114
|
+
|
|
115
|
+
## Performance
|
|
116
|
+
|
|
117
|
+
- Schema loading: Cached per class after first use
|
|
118
|
+
- Validation overhead: ~1-5ms when schemas defined
|
|
119
|
+
- Logging overhead: ~0.1ms per call
|
|
120
|
+
- Total framework overhead: < 10ms per service call
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# @title Core / 3. Service Objects
|
|
2
|
+
|
|
3
|
+
# Service Objects
|
|
4
|
+
|
|
5
|
+
Service objects encapsulate one business operation into a testable, reusable class. They sit between controllers and models, handling orchestration logic that doesn't belong in either.
|
|
6
|
+
|
|
7
|
+
## The Pattern
|
|
8
|
+
|
|
9
|
+
Services implement two methods: `initialize` (sets up dependencies) and `call` (executes business logic). All services return a `Response` object indicating success or failure.
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
module Users
|
|
13
|
+
module Create
|
|
14
|
+
class Service < Servus::Base
|
|
15
|
+
def initialize(email:, name:)
|
|
16
|
+
@email = email
|
|
17
|
+
@name = name
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def call
|
|
21
|
+
return failure("Email taken") if User.exists?(email: @email)
|
|
22
|
+
|
|
23
|
+
user = User.create!(email: @email, name: @name)
|
|
24
|
+
send_welcome_email(user)
|
|
25
|
+
|
|
26
|
+
success(user: user)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Usage
|
|
33
|
+
result = Users::Create::Service.call(email: "user@example.com", name: "John")
|
|
34
|
+
result.success? # => true
|
|
35
|
+
result.data[:user] # => #<User>
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Service Composition
|
|
39
|
+
|
|
40
|
+
Services can call other services. Use the returned Response to decide whether to continue or propagate the failure.
|
|
41
|
+
|
|
42
|
+
```ruby
|
|
43
|
+
def call
|
|
44
|
+
user_result = Users::Create::Service.call(user_params)
|
|
45
|
+
return user_result unless user_result.success? # propogates result failure
|
|
46
|
+
|
|
47
|
+
account_result = Accounts::Create::Service.call(
|
|
48
|
+
user: user_result.data[:user],
|
|
49
|
+
plan: @plan
|
|
50
|
+
)
|
|
51
|
+
return account_result unless account_result.success? # propogates result failure
|
|
52
|
+
|
|
53
|
+
success(
|
|
54
|
+
user: user_result.data[:user],
|
|
55
|
+
account: account_result.data[:account]
|
|
56
|
+
)
|
|
57
|
+
end
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## When to Extract to Services
|
|
61
|
+
|
|
62
|
+
**Extract when**:
|
|
63
|
+
- Logic spans multiple models
|
|
64
|
+
- Complex conditional branching
|
|
65
|
+
- External API calls
|
|
66
|
+
- Background processing needed
|
|
67
|
+
- Testing requires extensive setup
|
|
68
|
+
|
|
69
|
+
**Don't extract when**:
|
|
70
|
+
- Simple CRUD operations
|
|
71
|
+
- Single-model updates
|
|
72
|
+
- Logic naturally belongs in model
|
|
73
|
+
|
|
74
|
+
## Directory Structure
|
|
75
|
+
|
|
76
|
+
Each service lives in its own namespace to avoid naming collisions and allow for support classes.
|
|
77
|
+
|
|
78
|
+
```
|
|
79
|
+
app/services/
|
|
80
|
+
├── users/
|
|
81
|
+
│ └── create/
|
|
82
|
+
│ ├── service.rb
|
|
83
|
+
│ └── support/
|
|
84
|
+
│ └── welcome_email.rb
|
|
85
|
+
└── orders/
|
|
86
|
+
└── process/
|
|
87
|
+
├── service.rb
|
|
88
|
+
└── support/
|
|
89
|
+
├── payment_gateway.rb
|
|
90
|
+
└── inventory_updater.rb
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Support classes are private to their service - they should never be used by other services.
|
|
94
|
+
|
|
95
|
+
## Testing
|
|
96
|
+
|
|
97
|
+
Services are designed for easy testing with explicit inputs and outputs.
|
|
98
|
+
|
|
99
|
+
```ruby
|
|
100
|
+
RSpec.describe Users::Create::Service do
|
|
101
|
+
describe ".call" do
|
|
102
|
+
context "with valid params" do
|
|
103
|
+
it "creates user" do
|
|
104
|
+
result = described_class.call(email: "test@example.com", name: "Test")
|
|
105
|
+
expect(result.success?).to be true
|
|
106
|
+
expect(result.data[:user]).to be_persisted
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
context "with duplicate email" do
|
|
111
|
+
before { create(:user, email: "test@example.com") }
|
|
112
|
+
|
|
113
|
+
it "returns failure" do
|
|
114
|
+
result = described_class.call(email: "test@example.com", name: "Test")
|
|
115
|
+
expect(result.success?).to be false
|
|
116
|
+
expect(result.error.message).to eq("Email taken")
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
```
|