rails_application_service 0.7.0 → 0.8.0
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/CHANGELOG.md +6 -0
- data/README.md +28 -2
- data/lib/application_service/version.rb +1 -1
- data/lib/application_service.rb +25 -4
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b158e2b94c0e80a6f1dc41cccd12df44763fec3ab42a950b8340b0fa8eb569de
|
|
4
|
+
data.tar.gz: 3ba2fde9f168a75b5fc46f5c78f421279acc09252d99db7f64020070ab380bd2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 81c3469d0b7e5ba2821654f582fa2dda696cae04bb2226e480b7917a66b5a5eed59b323f257d0cfcf2bdb0f3a10e98537e5d0cffa53d8fde7e455556495871fb
|
|
7
|
+
data.tar.gz: 04fc085e5ba2efb03dd366235e556522734780b9d013ee0409b8efdd10bca243944fc1f406982b1c9b98d9c384d41930616f64ee91ffa4ef6908d88a6422712e
|
data/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,12 @@ All notable changes to this project are documented in this file.
|
|
|
5
5
|
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [0.8.0] - 2026-11-28
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
- `abstract_class!` declares a service class as abstract, so it cannot be instantiated or called,
|
|
12
|
+
and `abstract_class?` tells whether a class was declared abstract
|
|
13
|
+
|
|
8
14
|
## [0.7.0] - 2026-11-23
|
|
9
15
|
|
|
10
16
|
### Changed
|
data/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# Rails Application Service
|
|
2
2
|
|
|
3
|
+
[](https://rubygems.org/gems/rails_application_service)
|
|
4
|
+
|
|
3
5
|
This Ruby gem adds service classes (e.g., use cases, features, or commands) to Ruby on Rails web applications.
|
|
4
6
|
|
|
5
7
|
## Instructions
|
|
@@ -26,7 +28,8 @@ mkdir -p app/services
|
|
|
26
28
|
|
|
27
29
|
## Usage examples
|
|
28
30
|
|
|
29
|
-
The `ApplicationService::Base` class provides a standard interface for calling services.
|
|
31
|
+
The `ApplicationService::Base` class provides a standard interface for calling services.
|
|
32
|
+
It leverages `ActiveModel` for initialization with keyword arguments and input validation.
|
|
30
33
|
|
|
31
34
|
### Basic service example
|
|
32
35
|
|
|
@@ -58,6 +61,29 @@ sum = Sum.call(number_a: 1, number_b: 2) # => 3
|
|
|
58
61
|
|
|
59
62
|
`Sum.call` returns `false` when validation fails. A service that does not define `call` raises `ApplicationService::NotImplementedError`.
|
|
60
63
|
|
|
64
|
+
### Example with an abstract base service
|
|
65
|
+
|
|
66
|
+
Like `self.abstract_class = true` in ActiveRecord, `abstract_class!` declares a base class for your services
|
|
67
|
+
that cannot be instantiated or called itself. It is not inherited, so its subclasses stay concrete.
|
|
68
|
+
|
|
69
|
+
```ruby
|
|
70
|
+
class BaseService < ApplicationService::Base
|
|
71
|
+
abstract_class!
|
|
72
|
+
|
|
73
|
+
# Shared behavior for all services
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
class MyService < BaseService
|
|
77
|
+
def call
|
|
78
|
+
# Perform the service action
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
BaseService.abstract_class? # => true
|
|
83
|
+
BaseService.call # raises ApplicationService::NotImplementedError
|
|
84
|
+
MyService.call # nil
|
|
85
|
+
```
|
|
86
|
+
|
|
61
87
|
## Support
|
|
62
88
|
|
|
63
|
-
You can contact me at mariomarroquim@gmail.com.
|
|
89
|
+
You can contact me at [mariomarroquim@gmail.com](mailto:mariomarroquim@gmail.com).
|
data/lib/application_service.rb
CHANGED
|
@@ -7,7 +7,7 @@ require "active_model"
|
|
|
7
7
|
# Service objects encapsulate business logic and are typically used to
|
|
8
8
|
# perform a single action or a series of related actions.
|
|
9
9
|
module ApplicationService
|
|
10
|
-
# Raised when
|
|
10
|
+
# Raised when an abstract class (such as Base) is instantiated, or when a
|
|
11
11
|
# subclass does not implement `call`. Unlike Ruby's built-in
|
|
12
12
|
# NotImplementedError (a ScriptError), it is a StandardError, so a plain
|
|
13
13
|
# `rescue` catches it.
|
|
@@ -30,16 +30,37 @@ module ApplicationService
|
|
|
30
30
|
# end
|
|
31
31
|
#
|
|
32
32
|
# sum = Sum.call(number_a: 1, number_b: 2) # => 3
|
|
33
|
+
#
|
|
34
|
+
# Applications can declare their own abstract base with `abstract_class!`:
|
|
35
|
+
#
|
|
36
|
+
# class BaseService < ApplicationService::Base
|
|
37
|
+
# abstract_class!
|
|
38
|
+
# end
|
|
33
39
|
class Base
|
|
34
40
|
include ::ActiveModel::Model
|
|
35
41
|
|
|
42
|
+
class << self
|
|
43
|
+
# Declares the class as abstract, so it cannot be instantiated or called.
|
|
44
|
+
# Like ActiveRecord's `abstract_class`, it is not inherited by subclasses.
|
|
45
|
+
def abstract_class!
|
|
46
|
+
@abstract_class = true
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# @return [Boolean] Whether the class was declared abstract with `abstract_class!`.
|
|
50
|
+
def abstract_class?
|
|
51
|
+
@abstract_class == true
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
abstract_class!
|
|
56
|
+
|
|
36
57
|
# Initializes a new instance of the service object.
|
|
37
58
|
#
|
|
38
59
|
# @param kwargs [Hash] The attributes to be passed to the service object.
|
|
39
|
-
# @raise [ApplicationService::NotImplementedError] if
|
|
60
|
+
# @raise [ApplicationService::NotImplementedError] if the class was declared abstract.
|
|
40
61
|
def initialize(**kwargs)
|
|
41
|
-
if
|
|
42
|
-
raise NotImplementedError, "#{self.class
|
|
62
|
+
if self.class.abstract_class?
|
|
63
|
+
raise NotImplementedError, "#{self.class} is an abstract class and cannot be instantiated directly"
|
|
43
64
|
end
|
|
44
65
|
|
|
45
66
|
super
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rails_application_service
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.8.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Mário Marroquim
|
|
@@ -41,6 +41,7 @@ homepage: "https://github.com/mariomarroquim/rails_application_service"
|
|
|
41
41
|
licenses:
|
|
42
42
|
- MIT
|
|
43
43
|
metadata:
|
|
44
|
+
allowed_push_host: "https://rubygems.org"
|
|
44
45
|
homepage_uri: "https://github.com/mariomarroquim/rails_application_service"
|
|
45
46
|
changelog_uri: "https://github.com/mariomarroquim/rails_application_service/blob/main/CHANGELOG.md"
|
|
46
47
|
rubygems_mfa_required: "true"
|