boring-service 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +41 -0
- data/LICENSE.txt +21 -0
- data/README.md +110 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/boring-service.gemspec +23 -0
- data/lib/boring-service/hooks.rb +97 -0
- data/lib/boring-service/parameter.rb +43 -0
- data/lib/boring-service/version.rb +5 -0
- data/lib/boring-service.rb +144 -0
- data/spec/boring-service/parameter_spec.rb +38 -0
- data/spec/boring_service_spec.rb +50 -0
- data/spec/spec_helper.rb +3 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0fff58d8db851aab81fb4f77ffd8e02f96590a45087e7dd77f9dbaca4d64ff50
|
4
|
+
data.tar.gz: 27c581fe0603c0e83142b6a12dcf13ce1855806804c081a59f2e6b181f884312
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e12a2151f94f8c4971a1269ab3757fa37493b992ef452fa257284fb39753c182b0472086118d8dc7eed2bdef89db9bcd479e9e7ef4d91e3a97d6b7c76797fed8
|
7
|
+
data.tar.gz: 836aa30bfa6e481c7e4f4b5dd06bf2e53cfbfa1c30d28b80db4fdc82ee3e1fb0a6a19f2847d68cbdd510a52267117980753ea74ed4a1dc340da1a2bb01b289a9
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at michele.piccirillo@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
boring-service (1.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
coderay (1.1.3)
|
10
|
+
diff-lcs (1.5.1)
|
11
|
+
method_source (1.0.0)
|
12
|
+
pry (0.14.2)
|
13
|
+
coderay (~> 1.1)
|
14
|
+
method_source (~> 1.0)
|
15
|
+
rake (13.1.0)
|
16
|
+
rspec (3.13.0)
|
17
|
+
rspec-core (~> 3.13.0)
|
18
|
+
rspec-expectations (~> 3.13.0)
|
19
|
+
rspec-mocks (~> 3.13.0)
|
20
|
+
rspec-core (3.13.0)
|
21
|
+
rspec-support (~> 3.13.0)
|
22
|
+
rspec-expectations (3.13.0)
|
23
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
+
rspec-support (~> 3.13.0)
|
25
|
+
rspec-mocks (3.13.0)
|
26
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
27
|
+
rspec-support (~> 3.13.0)
|
28
|
+
rspec-support (3.13.1)
|
29
|
+
|
30
|
+
PLATFORMS
|
31
|
+
arm64-darwin-22
|
32
|
+
|
33
|
+
DEPENDENCIES
|
34
|
+
boring-service!
|
35
|
+
bundler (>= 1.12)
|
36
|
+
pry
|
37
|
+
rake (>= 10.0)
|
38
|
+
rspec (~> 3.6)
|
39
|
+
|
40
|
+
BUNDLED WITH
|
41
|
+
2.4.10
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Jeffrey Dill
|
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,110 @@
|
|
1
|
+
# BoringService
|
2
|
+
|
3
|
+
Provides a lightweight, standard (quite frankly, boring) implementation for the service-object pattern.
|
4
|
+
|
5
|
+
The benefits of using this over a [PORO](https://en.wikipedia.org/wiki/Plain_old_Java_object), besides having a standard way to define and run service-object methods, is the addition of parameter type-checking and before hooks.
|
6
|
+
|
7
|
+
In every other way, this leaves you with vanilla, semantic Ruby. Boring is powerful.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'boring-service'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install boring-service
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### Parameters
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class CalculationService < BoringService
|
31
|
+
# Type checking is optional. If omitted, anything is accepted.
|
32
|
+
# Type checking can also be done with a proc or anything that responds to #===
|
33
|
+
# e.g. parameter :start_number, ->(p) { p.respond_to?(:to_i) }
|
34
|
+
parameter :start_number, Integer
|
35
|
+
|
36
|
+
# Parameters that define a default are optional.
|
37
|
+
# `default` supports also a proc, that gets evaluated at
|
38
|
+
# object instantiation.
|
39
|
+
# e.g. default: -> { Time.now }
|
40
|
+
parameter :end_number, Integer, default: 2
|
41
|
+
|
42
|
+
def call
|
43
|
+
@magic_number = 42
|
44
|
+
perform_complex_calculation
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def perform_complex_calculation
|
50
|
+
# The arguments are available as accessors
|
51
|
+
start_number + end_number + @magic_number
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# The class-method version of `call` accepts the arguments as named parameters and, subsequently, calls
|
56
|
+
# the instance-method version of `call` (as it's defined in your service object).
|
57
|
+
CalculationService.call(start_number: 1, end_number: 3) #=> 46
|
58
|
+
CalculationService.call(start_number: 1) #=> 45
|
59
|
+
CalculationService.call(end_number: 3) #=> raise BoringService::ParameterRequired
|
60
|
+
```
|
61
|
+
|
62
|
+
### Hooks
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
class RandomService < BoringService
|
66
|
+
# Before hooks may be defined as a Symbol method name (which calls the named method) or as a block
|
67
|
+
before :set_start_time
|
68
|
+
before { puts "Started at #{@start_time}" }
|
69
|
+
|
70
|
+
def call
|
71
|
+
puts "Called"
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def set_start_time
|
77
|
+
@start_time = Time.now
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
RandomService.call
|
82
|
+
|
83
|
+
#=> "Started at 2024-03-26 13:40:11.466186 -0400"
|
84
|
+
#=> "Called"
|
85
|
+
```
|
86
|
+
|
87
|
+
|
88
|
+
### Errors
|
89
|
+
|
90
|
+
* `BoringService::ParameterRequired` is raised when a required parameter is not set on `call`
|
91
|
+
* `BoringService::InvalidParameterValue` is raised when a given value's type does not match the type specified for the parameter
|
92
|
+
* `BoringService::UnknownParameter` is raised when using an undefined parameter
|
93
|
+
|
94
|
+
All these classes inherit from `BoringService::ParameterError`, which inherits from `ArgumentError`.
|
95
|
+
|
96
|
+
|
97
|
+
## Development
|
98
|
+
|
99
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
100
|
+
|
101
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
102
|
+
|
103
|
+
## Contributing
|
104
|
+
|
105
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/LeadSimple/boring-service. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
106
|
+
|
107
|
+
|
108
|
+
## License
|
109
|
+
|
110
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "boringservice"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'boring-service/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "boring-service"
|
8
|
+
spec.version = BoringService::VERSION
|
9
|
+
spec.authors = ["LeadSimple Engineering"]
|
10
|
+
spec.email = ["engineering@leadsimple.com"]
|
11
|
+
spec.summary = 'A simple implementation of the service-object pattern'
|
12
|
+
spec.homepage = "https://github.com/LeadSimple/simple-service"
|
13
|
+
spec.license = "MIT"
|
14
|
+
spec.files = `find *`.split("\n").uniq.sort.select { |f| !f.empty? }
|
15
|
+
spec.test_files = spec.files.grep(/^spec/)
|
16
|
+
spec.require_paths = ["lib"]
|
17
|
+
spec.required_ruby_version = ">= 2.0"
|
18
|
+
|
19
|
+
spec.add_development_dependency "bundler", ">= 1.12"
|
20
|
+
spec.add_development_dependency "rake", ">= 10.0"
|
21
|
+
spec.add_development_dependency "pry"
|
22
|
+
spec.add_development_dependency 'rspec', '~> 3.6'
|
23
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
class BoringService
|
2
|
+
module Hooks
|
3
|
+
def self.included(base)
|
4
|
+
base.class_eval do
|
5
|
+
extend ClassMethods
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
# Public: Declare hooks to run before the service is called. The before method may be called multiple times, with
|
11
|
+
# subsequent calls being appended to the existing before hooks.
|
12
|
+
#
|
13
|
+
# hooks - Zero or more Symbol method names – the declared method will be called before service invocation.
|
14
|
+
# block - An optional block to be executed as a hook. If given, the block is executed after methods corresponding
|
15
|
+
# to any given Symbols.
|
16
|
+
#
|
17
|
+
# Examples
|
18
|
+
#
|
19
|
+
# class MyService < BoringService
|
20
|
+
# before :set_start_time
|
21
|
+
#
|
22
|
+
# before do
|
23
|
+
# puts "started at #{@start_time}"
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# def call
|
27
|
+
# puts "called"
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# private
|
31
|
+
#
|
32
|
+
# def set_start_time
|
33
|
+
# @start_time = Time.now
|
34
|
+
# end
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# Returns nothing.
|
38
|
+
def before(*hooks, &block)
|
39
|
+
hooks << block if block
|
40
|
+
hooks.each { |hook| before_hooks.push(hook) }
|
41
|
+
end
|
42
|
+
|
43
|
+
# Internal: An Array of declared hooks to run before Interactor
|
44
|
+
# invocation. The hooks appear in the order in which they will be run.
|
45
|
+
#
|
46
|
+
# Examples
|
47
|
+
#
|
48
|
+
# class MyInteractor
|
49
|
+
# include Interactor
|
50
|
+
#
|
51
|
+
# before :set_start_time, :say_hello
|
52
|
+
# end
|
53
|
+
#
|
54
|
+
# MyInteractor.before_hooks
|
55
|
+
# # => [:set_start_time, :say_hello]
|
56
|
+
#
|
57
|
+
# Returns an Array of Symbols and Procs.
|
58
|
+
def before_hooks
|
59
|
+
@before_hooks ||= []
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
# Internal: Run before hooks.
|
66
|
+
#
|
67
|
+
# Returns nothing.
|
68
|
+
def run_before_hooks
|
69
|
+
run_hooks(self.class.before_hooks)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Internal: Run a colection of hooks. The "run_hooks" method is the common
|
73
|
+
# interface by which collections of either before or after hooks are run.
|
74
|
+
#
|
75
|
+
# hooks - An Array of Symbol and Proc hooks.
|
76
|
+
#
|
77
|
+
# Returns nothing.
|
78
|
+
def run_hooks(hooks)
|
79
|
+
hooks.each { |hook| run_hook(hook) }
|
80
|
+
end
|
81
|
+
|
82
|
+
# Internal: Run an individual hook. The "run_hook" method is the common
|
83
|
+
# interface by which an individual hook is run. If the given hook is a
|
84
|
+
# symbol, the method is invoked whether public or private. If the hook is a
|
85
|
+
# proc, the proc is evaluated in the context of the current instance.
|
86
|
+
#
|
87
|
+
# hook - A Symbol or Proc hook.
|
88
|
+
# args - Zero or more arguments to be passed as block arguments into the
|
89
|
+
# given block or as arguments into the method described by the given
|
90
|
+
# Symbol method name.
|
91
|
+
#
|
92
|
+
# Returns nothing.
|
93
|
+
def run_hook(hook, *args)
|
94
|
+
hook.is_a?(Symbol) ? send(hook, *args) : instance_exec(*args, &hook)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class BoringService
|
4
|
+
class Parameter
|
5
|
+
def initialize(name, type, options = {})
|
6
|
+
invalid_options = options.keys - [:default]
|
7
|
+
raise ArgumentError, "Invalid keys: #{invalid_options.inspect}. Valid keys are: :default" \
|
8
|
+
unless invalid_options.empty?
|
9
|
+
|
10
|
+
@name = name
|
11
|
+
@type = type
|
12
|
+
@options = options
|
13
|
+
|
14
|
+
freeze
|
15
|
+
end
|
16
|
+
|
17
|
+
attr_reader :name, :type
|
18
|
+
|
19
|
+
def default?
|
20
|
+
@options.key?(:default)
|
21
|
+
end
|
22
|
+
|
23
|
+
def default
|
24
|
+
@options[:default]
|
25
|
+
end
|
26
|
+
|
27
|
+
def default_in(context)
|
28
|
+
if @options[:default].respond_to?(:call)
|
29
|
+
context.instance_exec(&@options[:default])
|
30
|
+
else
|
31
|
+
@options[:default]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def acceptable?(value)
|
36
|
+
(value.nil? && nullable?) || type === value # rubocop:disable Style/CaseEquality
|
37
|
+
end
|
38
|
+
|
39
|
+
def nullable?
|
40
|
+
default? && default.nil?
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,144 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "boring-service/hooks"
|
4
|
+
require 'boring-service/parameter'
|
5
|
+
|
6
|
+
# Provides a standard implementation for a service-object approach to abstracting large methods.
|
7
|
+
#
|
8
|
+
# @example Defining and invoking a method object
|
9
|
+
# class ComplexCalculation < BoringService
|
10
|
+
# parameter :start_number, Integer
|
11
|
+
# parameter :end_number, Integer, default: 2
|
12
|
+
#
|
13
|
+
# def call
|
14
|
+
# @magic_number = 42
|
15
|
+
# perform_complex_calculation
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# private
|
19
|
+
#
|
20
|
+
# def perform_complex_calculation
|
21
|
+
# start_number + second_number + @magic_number
|
22
|
+
# end
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# ComplexCalculation.call(start_number: 1, end_number: 3) #=> 46
|
26
|
+
# ComplexCalculation.call(start_number: 1) #=> 45
|
27
|
+
# ComplexCalculation.call(end_number: 3) #=> raise BoringService::UnknownParameter
|
28
|
+
#
|
29
|
+
# calculation = ComplexCalculation.new(end_number: 3)
|
30
|
+
# calculation.start_number = 1
|
31
|
+
# calculation.call # => 46
|
32
|
+
#
|
33
|
+
# calculation.end_number = 2
|
34
|
+
# calculation.call # => 45
|
35
|
+
#
|
36
|
+
class BoringService
|
37
|
+
ParameterError = Class.new(::ArgumentError)
|
38
|
+
ParameterRequired = Class.new(ParameterError)
|
39
|
+
InvalidParameterValue = Class.new(ParameterError)
|
40
|
+
UnknownParameter = Class.new(ParameterError)
|
41
|
+
|
42
|
+
include Hooks
|
43
|
+
|
44
|
+
class << self
|
45
|
+
# Calls the BoringService with the given arguments.
|
46
|
+
#
|
47
|
+
# @param **args [Hash{Symbol => Object}] arguments to pass to the method
|
48
|
+
# @return [Object] return value of the method object
|
49
|
+
def call(**args)
|
50
|
+
new(**args).run
|
51
|
+
end
|
52
|
+
|
53
|
+
protected
|
54
|
+
|
55
|
+
# Defines a parameter for the service object.
|
56
|
+
#
|
57
|
+
# Parameters are also inherited from superclasses and can be redefined (overwritten) in subclasses.
|
58
|
+
#
|
59
|
+
# @param name [Symbol] name of the parameter
|
60
|
+
# @param type [Class, #===] type of the parameter. Can be a Class, a Proc or anything that defines a meaningful
|
61
|
+
# `===` method
|
62
|
+
# @param **options [Hash] extra options for the parameter
|
63
|
+
# @option **options [Object, #call] :default default value if the parameter is not passed. If the default implements
|
64
|
+
# `#call`, it gets called once in the context of the method object instance when it is instantiated.
|
65
|
+
#
|
66
|
+
# @return [void]
|
67
|
+
def parameter(name, type = BasicObject, **options)
|
68
|
+
arg = BoringService::Parameter.new(name, type, options)
|
69
|
+
parameters.delete(arg)
|
70
|
+
parameters << arg
|
71
|
+
|
72
|
+
define_method("#{name}=") do |value|
|
73
|
+
raise InvalidParameterValue, "Expected a #{type} for #{name}, #{value.class} received" unless arg.acceptable?(value)
|
74
|
+
instance_variable_set("@#{name}", value)
|
75
|
+
end
|
76
|
+
|
77
|
+
define_method("#{name}?") do
|
78
|
+
!public_send(name).nil?
|
79
|
+
end
|
80
|
+
|
81
|
+
attr_reader name
|
82
|
+
end
|
83
|
+
|
84
|
+
private
|
85
|
+
|
86
|
+
# @return [Set]
|
87
|
+
def superclass_parameters
|
88
|
+
if superclass < BoringService
|
89
|
+
superclass.send(:parameters)
|
90
|
+
else
|
91
|
+
Set.new
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# @return [Set]
|
96
|
+
def parameters
|
97
|
+
@parameters ||= Set.new(superclass_parameters)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# @param **args [Hash{Symbol => Object}] arguments to set on the method object
|
102
|
+
def initialize(**args)
|
103
|
+
self.class.send(:parameters).freeze
|
104
|
+
|
105
|
+
args.each do |k, v|
|
106
|
+
method = "#{k}="
|
107
|
+
raise UnknownParameter, "Parameter #{k} unknown" unless respond_to?(method)
|
108
|
+
public_send(method, v)
|
109
|
+
end
|
110
|
+
|
111
|
+
self.class.send(:parameters)
|
112
|
+
.reject { |p| args.keys.map(&:to_sym).include?(p.name) }
|
113
|
+
.select(&:default?)
|
114
|
+
.each { |p| public_send("#{p.name}=", p.default_in(self)) }
|
115
|
+
end
|
116
|
+
|
117
|
+
def run
|
118
|
+
assert_required_arguments!
|
119
|
+
run_before_hooks
|
120
|
+
call
|
121
|
+
end
|
122
|
+
|
123
|
+
# Calls the method object with the parameters currently set.
|
124
|
+
# @raise [ArgumentError] if any required parameter is missing
|
125
|
+
# @return [Object] the return value result of the method invokation
|
126
|
+
def call
|
127
|
+
# This method is expected to be overridden. If it isn't, this error will be raised.
|
128
|
+
raise NotImplementedError, 'Implementation missing. Please use `def call; end` to define method body'
|
129
|
+
end
|
130
|
+
|
131
|
+
private
|
132
|
+
|
133
|
+
# @raise [ParameterRequired]
|
134
|
+
def assert_required_arguments!
|
135
|
+
missing_params =
|
136
|
+
self.class.send(:parameters)
|
137
|
+
.select { |p| !p.default? && !public_send("#{p.name}?") }
|
138
|
+
.map(&:name)
|
139
|
+
|
140
|
+
unless missing_params.empty?
|
141
|
+
raise ParameterRequired, "Missing required arguments: #{missing_params.join(', ')}"
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe BoringService::Parameter do
|
4
|
+
describe "#default?" do
|
5
|
+
context "when the defined parameter includes a default" do
|
6
|
+
parameter = described_class.new(:foo, String, default: "bar")
|
7
|
+
|
8
|
+
it { expect(parameter.default?).to be true }
|
9
|
+
end
|
10
|
+
|
11
|
+
context "when the defined parameter doesn't include a default" do
|
12
|
+
parameter = described_class.new(:foo, String)
|
13
|
+
|
14
|
+
it { expect(parameter.default?).to be false }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#default' do
|
19
|
+
parameter = described_class.new(:foo, String, default: 'bar')
|
20
|
+
|
21
|
+
it { expect(parameter.default).to eq 'bar' }
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "#acceptable?" do
|
25
|
+
parameter = described_class.new(:number_string, String)
|
26
|
+
|
27
|
+
it { expect(parameter.acceptable?("five")).to be true }
|
28
|
+
it { expect(parameter.acceptable?(5)).to be false }
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#nullable?" do
|
32
|
+
parameter_with_no_default = described_class.new(:foo, String)
|
33
|
+
parameter_with_nil_default = described_class.new(:foo, String, default: nil)
|
34
|
+
|
35
|
+
it { expect(parameter_with_no_default.nullable?).to be false }
|
36
|
+
it { expect(parameter_with_nil_default.nullable?).to be true }
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class CustomService < BoringService
|
4
|
+
parameter :required_param, String
|
5
|
+
parameter :optional_param, String, default: 'something'
|
6
|
+
|
7
|
+
before :first_pre_hook
|
8
|
+
before { @second_pre_hook_time = Time.now }
|
9
|
+
|
10
|
+
def call
|
11
|
+
{
|
12
|
+
first_pre_hook_time: @first_pre_hook_time,
|
13
|
+
second_pre_hook_time: @second_pre_hook_time
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def first_pre_hook
|
20
|
+
@first_pre_hook_time = Time.now
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
RSpec.describe BoringService do
|
25
|
+
describe ".call" do
|
26
|
+
it "should raise an error if a required param is not passed" do
|
27
|
+
expect { CustomService.call }.to raise_error BoringService::ParameterRequired
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should raise an error if a param is the wrong type" do
|
31
|
+
expect { CustomService.call(required_param: 5) }.to raise_error BoringService::InvalidParameterValue
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should raise an error if an undefined param is passed" do
|
35
|
+
expect { CustomService.call(undefined_param: 5) }.to raise_error BoringService::UnknownParameter
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should call before hooks in the order that they're defined" do
|
39
|
+
response = CustomService.call(required_param: "foo")
|
40
|
+
|
41
|
+
expect(response[:first_pre_hook_time] < response[:second_pre_hook_time]).to be true
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should throw an error if the `.call` method is not overridden" do
|
45
|
+
class BadService < BoringService; end
|
46
|
+
|
47
|
+
expect { BadService.call }.to raise_error NotImplementedError
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: boring-service
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- LeadSimple Engineering
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-08-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.6'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.6'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- engineering@leadsimple.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- CODE_OF_CONDUCT.md
|
77
|
+
- Gemfile
|
78
|
+
- Gemfile.lock
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- boring-service.gemspec
|
85
|
+
- lib/boring-service.rb
|
86
|
+
- lib/boring-service/hooks.rb
|
87
|
+
- lib/boring-service/parameter.rb
|
88
|
+
- lib/boring-service/version.rb
|
89
|
+
- spec/boring-service/parameter_spec.rb
|
90
|
+
- spec/boring_service_spec.rb
|
91
|
+
- spec/spec_helper.rb
|
92
|
+
homepage: https://github.com/LeadSimple/simple-service
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '2.0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubygems_version: 3.4.10
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: A simple implementation of the service-object pattern
|
115
|
+
test_files:
|
116
|
+
- spec/boring-service/parameter_spec.rb
|
117
|
+
- spec/boring_service_spec.rb
|
118
|
+
- spec/spec_helper.rb
|