simple_base_service 0.1.2
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 +7 -0
- data/.rubocop.yml +25 -0
- data/CHANGELOG.md +13 -0
- data/CODE_OF_CONDUCT.md +10 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +115 -0
- data/Rakefile +10 -0
- data/lib/simple_service/base.rb +59 -0
- data/lib/simple_service/logger.rb +76 -0
- data/lib/simple_service/version.rb +5 -0
- data/lib/simple_service.rb +10 -0
- data/sig/simple_service.rbs +4 -0
- metadata +56 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 2c790803fc2653a662ff7a3a034d8059d67c3ec9157773b6a73402d18d1ff2cf
|
|
4
|
+
data.tar.gz: 97136dbfd438d00ebc11ecce6190578ec2fb029439300005e4fa38b1ca59f9a4
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 3cd5c8ea3aa331bbe0c7197d1e1993103eac9c43f1c04cfdeaf9ae39c1efd9b87f474300dae82b368067b5f4cbf96f3babada7839f7ddfd00911485ad9d544c7
|
|
7
|
+
data.tar.gz: dd0326a322f5277a03315cd174976f427e9565a3c310bdb322edb8570afda427bf0a56d7fcbadb20f3b95e1056dc4b9e98e8ec23650a73c4254f9dcfedc11b80
|
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Use Standard's RuboCop configuration as the base so `rubocop` follows Standard
|
|
2
|
+
# See https://github.com/testdouble/standard
|
|
3
|
+
|
|
4
|
+
AllCops:
|
|
5
|
+
NewCops: enable
|
|
6
|
+
TargetRubyVersion: 3.2
|
|
7
|
+
SuggestExtensions: false
|
|
8
|
+
|
|
9
|
+
inherit_gem:
|
|
10
|
+
standard: config/base.yml
|
|
11
|
+
|
|
12
|
+
# Enable RuboCop plugins for RSpec and Rake
|
|
13
|
+
plugins:
|
|
14
|
+
- rubocop-rspec
|
|
15
|
+
- rubocop-rake
|
|
16
|
+
|
|
17
|
+
# Opt into new cops from RuboCop RSpec
|
|
18
|
+
RSpec/IncludeExamples:
|
|
19
|
+
Enabled: true
|
|
20
|
+
RSpec/LeakyLocalVariable:
|
|
21
|
+
Enabled: true
|
|
22
|
+
RSpec/Output:
|
|
23
|
+
Enabled: true
|
|
24
|
+
|
|
25
|
+
# You may add project-specific overrides below if necessary.
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Code of Conduct
|
|
2
|
+
|
|
3
|
+
"simple_base_service" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
|
|
4
|
+
|
|
5
|
+
* Participants will be tolerant of opposing views.
|
|
6
|
+
* Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
|
|
7
|
+
* When interpreting the words and actions of others, participants should always assume good intentions.
|
|
8
|
+
* Behaviour which can be reasonably considered harassment will not be tolerated.
|
|
9
|
+
|
|
10
|
+
If you have any concerns about behaviour within this project, please contact us at ["emseh@protonmail.com"](mailto:"emseh@protonmail.com").
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ModeraX
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ModeraX
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# SimpleService
|
|
2
|
+
|
|
3
|
+
A small framework for building plain Ruby "service objects" with a consistent interface.
|
|
4
|
+
|
|
5
|
+
This gem provides:
|
|
6
|
+
|
|
7
|
+
- A `Base` class for services with `.run` and `#call` conventions
|
|
8
|
+
- Lightweight error handling (`add_error`, `success?`, `failure?`)
|
|
9
|
+
- Pluggable logging (instance-level, class-level, `:rails`, and `:stdout` options)
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
Install the gem from RubyGems (recommended) or add it from GitHub for development. Examples are shown below.
|
|
14
|
+
|
|
15
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
bundle add simple_base_service
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
# Gemfile
|
|
25
|
+
gem 'simple_base_service', github: 'moderax/simple_base_service'
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Then run `bundle install`.
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
Define a service by inheriting from `SimpleService::Base` and implementing `#call`:
|
|
33
|
+
|
|
34
|
+
```ruby
|
|
35
|
+
class MyService < SimpleService::Base
|
|
36
|
+
def call
|
|
37
|
+
log('starting')
|
|
38
|
+
|
|
39
|
+
# perform work
|
|
40
|
+
if something_went_wrong
|
|
41
|
+
add_error('something bad happened')
|
|
42
|
+
return
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
:ok
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Execute the service (class-level convenience)
|
|
50
|
+
MyService.run
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Inspecting result and errors
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
service = MyService.new
|
|
57
|
+
service.call
|
|
58
|
+
if service.failure?
|
|
59
|
+
puts service.errors
|
|
60
|
+
end
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Logging
|
|
64
|
+
|
|
65
|
+
You can control logging per call, per instance, or globally:
|
|
66
|
+
|
|
67
|
+
- Pass an object that responds to `info(message)` as `logger: my_logger` to `MyService.run` or `MyService.new`
|
|
68
|
+
- Pass `logger: :rails` to use `Rails.logger` when available (falls back to STDOUT)
|
|
69
|
+
- Pass `logger: :stdout` to force a simple STDOUT logger
|
|
70
|
+
- Set a class-level default with `SimpleService::Base.logger = :stdout` or `MyService.logger = my_logger`
|
|
71
|
+
|
|
72
|
+
Logger resolution order:
|
|
73
|
+
|
|
74
|
+
1. Instance `@logger` (if set)
|
|
75
|
+
2. Class-level `logger` (inheritable by subclasses)
|
|
76
|
+
3. `Rails.logger` (if available)
|
|
77
|
+
4. Fallback `Logger.new($stdout)`
|
|
78
|
+
|
|
79
|
+
### Example: Rails integration
|
|
80
|
+
|
|
81
|
+
In Rails apps, using `logger: :rails` will forward logs to `Rails.logger` when present:
|
|
82
|
+
|
|
83
|
+
```ruby
|
|
84
|
+
MyService.run(logger: :rails)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Development
|
|
88
|
+
|
|
89
|
+
- Install dependencies: `bin/setup` (bundler will install gems)
|
|
90
|
+
- Run tests: `bundle exec rspec`
|
|
91
|
+
- Lint: `bundle exec standardrb` and `bundle exec rubocop`
|
|
92
|
+
|
|
93
|
+
## Release
|
|
94
|
+
|
|
95
|
+
- Bump the version in `lib/simple_base_service/version.rb`
|
|
96
|
+
- Run `bundle exec rake release` (this tags and pushes a gem to RubyGems)
|
|
97
|
+
|
|
98
|
+
## Contributing
|
|
99
|
+
|
|
100
|
+
Bug reports and pull requests are welcome. Please follow these steps:
|
|
101
|
+
|
|
102
|
+
1. Fork the repository
|
|
103
|
+
2. Create a branch for your change
|
|
104
|
+
3. Add tests and update the README if you change behavior
|
|
105
|
+
4. Open a pull request
|
|
106
|
+
|
|
107
|
+
We use `standardrb` and `rubocop` for linting—PRs should pass linters and specs.
|
|
108
|
+
|
|
109
|
+
## License
|
|
110
|
+
|
|
111
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
112
|
+
|
|
113
|
+
## Code of Conduct
|
|
114
|
+
|
|
115
|
+
Everyone interacting in the SimpleService project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/moderax/simple_base_service/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SimpleService
|
|
4
|
+
# Base class for all application services providing a standard interface for running service methods
|
|
5
|
+
class Base
|
|
6
|
+
attr_reader :errors, :args
|
|
7
|
+
|
|
8
|
+
# Main entry point to execute a method from the class level
|
|
9
|
+
def self.run(method_name = :call, **args, &)
|
|
10
|
+
instance = new(**args, &)
|
|
11
|
+
|
|
12
|
+
# Passing 'true' as the second argument allows respond_to? to check for private methods as well
|
|
13
|
+
unless instance.respond_to?(method_name, true)
|
|
14
|
+
raise NoMethodError, "Method :#{method_name} is not implemented or not found in #{name}"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
instance.send(method_name)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Initialize the service with flexible arguments and auto-set instance variables
|
|
21
|
+
def initialize(**args)
|
|
22
|
+
@errors = []
|
|
23
|
+
@args = args
|
|
24
|
+
|
|
25
|
+
# Automatically set instance variables for each key in args
|
|
26
|
+
args.each do |key, value|
|
|
27
|
+
instance_variable_set("@#{key}", value) if key.to_s.match?(/\A[a-z_][a-z0-9_]*\z/i)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Default method to be overridden by subclasses
|
|
32
|
+
def call
|
|
33
|
+
raise NotImplementedError, "#{self.class.name} must implement the #call method"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
include SimpleService::Logger
|
|
39
|
+
|
|
40
|
+
private :logger, :log
|
|
41
|
+
|
|
42
|
+
# Check if there are any accumulated errors
|
|
43
|
+
def success?
|
|
44
|
+
@errors.empty?
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Check if any errors have occurred
|
|
48
|
+
def failure?
|
|
49
|
+
!success?
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Helper to add error messages to the collection uniquely
|
|
53
|
+
def add_error(message)
|
|
54
|
+
return if message.nil?
|
|
55
|
+
|
|
56
|
+
@errors << message unless @errors.include?(message)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SimpleService
|
|
4
|
+
# Extracted logging behavior so services can include or override it easily.
|
|
5
|
+
module Logger
|
|
6
|
+
def self.included(base)
|
|
7
|
+
base.extend(ClassMethods)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# rubocop:disable Style/Documentation
|
|
11
|
+
module ClassMethods
|
|
12
|
+
def logger
|
|
13
|
+
return @logger if defined?(@logger) && @logger
|
|
14
|
+
return superclass.logger if superclass.respond_to?(:logger)
|
|
15
|
+
|
|
16
|
+
nil
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def logger=(value)
|
|
20
|
+
@logger = value
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
# rubocop:enable Style/Documentation
|
|
24
|
+
|
|
25
|
+
# Returns the logger to use: instance-level override -> class-level -> Rails.logger -> STDOUT Logger
|
|
26
|
+
def logger
|
|
27
|
+
# try instance-level value
|
|
28
|
+
value = resolve_logger_value(defined?(@logger) && @logger)
|
|
29
|
+
return value if value
|
|
30
|
+
|
|
31
|
+
# try class-level value
|
|
32
|
+
value = resolve_logger_value(self.class.logger)
|
|
33
|
+
return value if value
|
|
34
|
+
|
|
35
|
+
# rails fallback then stdout
|
|
36
|
+
rails_logger || detected_stdout_logger
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def resolve_logger_value(value)
|
|
42
|
+
return nil unless value
|
|
43
|
+
|
|
44
|
+
case value
|
|
45
|
+
when :rails
|
|
46
|
+
rails_logger
|
|
47
|
+
when :stdout
|
|
48
|
+
detected_stdout_logger
|
|
49
|
+
else
|
|
50
|
+
value
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def rails_logger
|
|
55
|
+
return Rails.logger if defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
|
|
56
|
+
|
|
57
|
+
nil
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def detected_stdout_logger
|
|
61
|
+
require "logger"
|
|
62
|
+
@detected_stdout_logger ||= ::Logger.new($stdout)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Generic logging helper
|
|
66
|
+
def log(message)
|
|
67
|
+
prefix = "[#{self.class.name}]"
|
|
68
|
+
|
|
69
|
+
if logger.respond_to?(:info)
|
|
70
|
+
logger.info("#{prefix} #{message}")
|
|
71
|
+
else
|
|
72
|
+
puts("#{prefix} #{message}")
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: simple_base_service
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.2
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- ModeraX
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: Provides a `Base` class for service objects with `.run`/`#call`, simple
|
|
13
|
+
error handling, and configurable logging.
|
|
14
|
+
email:
|
|
15
|
+
- emseh@protonmail.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- ".rubocop.yml"
|
|
21
|
+
- CHANGELOG.md
|
|
22
|
+
- CODE_OF_CONDUCT.md
|
|
23
|
+
- LICENSE
|
|
24
|
+
- LICENSE.txt
|
|
25
|
+
- README.md
|
|
26
|
+
- Rakefile
|
|
27
|
+
- lib/simple_service.rb
|
|
28
|
+
- lib/simple_service/base.rb
|
|
29
|
+
- lib/simple_service/logger.rb
|
|
30
|
+
- lib/simple_service/version.rb
|
|
31
|
+
- sig/simple_service.rbs
|
|
32
|
+
homepage: https://github.com/moderax/simple_base_service
|
|
33
|
+
licenses:
|
|
34
|
+
- MIT
|
|
35
|
+
metadata:
|
|
36
|
+
homepage_uri: https://github.com/moderax/simple_base_service
|
|
37
|
+
source_code_uri: https://github.com/moderax/simple_base_service
|
|
38
|
+
changelog_uri: https://github.com/moderax/simple_base_service/CHANGELOG.md
|
|
39
|
+
rdoc_options: []
|
|
40
|
+
require_paths:
|
|
41
|
+
- lib
|
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: 3.2.0
|
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
48
|
+
requirements:
|
|
49
|
+
- - ">="
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: '0'
|
|
52
|
+
requirements: []
|
|
53
|
+
rubygems_version: 4.0.6
|
|
54
|
+
specification_version: 4
|
|
55
|
+
summary: Tiny framework for plain Ruby service objects
|
|
56
|
+
test_files: []
|