service_base 1.0.1 → 1.0.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 +4 -4
- data/README.md +38 -22
- data/lib/service_base/argument_type_annotations.rb +0 -4
- data/lib/service_base/service.rb +10 -16
- data/lib/service_base/version.rb +1 -1
- data/lib/service_base.rb +0 -1
- metadata +1 -2
- data/lib/service_base/types.rb +0 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 79ca15b6d1d305ea1dea4ab6633e63725de9f5d5f951c4221b9b59bef38b03ab
|
4
|
+
data.tar.gz: 3bb44ec63bd53452ac09c3fc0f531bf25218dcbb1434b5e8bd0a1dc3f8f75be0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a730fb152af189eb68c07bf5a55a31529605af58a4a8980afbf3369769ec0d3dcf81ab3e6d89ade5b9903dd547fa3f06b2db62edb9621d4ef8edfbab46c2d81
|
7
|
+
data.tar.gz: 2f666d22c5368a817de07145a1e27eb32faf98b326128c9b6c8192fa48eecda9d3d0dd6d36b0eaf8848c7f9c5340b770e726777c4e7f78f2550e0393208ffe94
|
data/README.md
CHANGED
@@ -165,13 +165,10 @@ The positional name and type arguments are required, the other options
|
|
165
165
|
are as follows.
|
166
166
|
`argument(:name, String, optional: true, description: "The User's name")`
|
167
167
|
|
168
|
-
If an argument is optional and has a default value, simply set
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
`.freeze` any mutable default values, ie.
|
173
|
-
`default: {}.freeze`. Failure to do so will raise an
|
174
|
-
`ArgumentError`.
|
168
|
+
If an argument is optional and has a default value, simply set `default: your_value` but do not also specify `optional: true`.
|
169
|
+
Doing so will raise an `ArgumentError`.
|
170
|
+
Additionally, be sure to `.freeze` any mutable default values, ie. `default: {}.freeze`.
|
171
|
+
Failure to do so will raise an `ArgumentError`.
|
175
172
|
|
176
173
|
Empty strings attempted to coerce into integers will throw an error.
|
177
174
|
See [https://github.com/dry-rb/dry-types/issues/344#issuecomment-518743661](https://github.com/dry-rb/dry-types/issues/344#issuecomment-518743661)
|
@@ -187,32 +184,53 @@ class MyService < ServiceBase::Service
|
|
187
184
|
end
|
188
185
|
```
|
189
186
|
|
190
|
-
To get the full hash of `argument`s passed into a service,
|
191
|
-
use `
|
187
|
+
To get the full hash of `argument`'s keys and values passed into a service,
|
188
|
+
use `arguments`. This is a very useful technique for
|
192
189
|
services that update an object. For example
|
193
190
|
|
194
191
|
```ruby
|
195
192
|
class User::UpdateService < ServiceBase::Service
|
193
|
+
argument(:name, String)
|
194
|
+
|
196
195
|
def call
|
197
|
-
user.update(
|
196
|
+
user.update(arguments)
|
198
197
|
end
|
199
198
|
end
|
200
199
|
```
|
201
200
|
|
202
|
-
|
201
|
+
## Types
|
202
|
+
|
203
|
+
Argument types come from, [Dry.rb’s Types](https://dry-rb.org/gems/dry-types/1.2/built-in-types/), which can be extended.
|
204
|
+
You may also add custom types as outlined in [Dry.rb Custom Types](https://dry-rb.org/gems/dry-types/1.2/custom-types/).
|
205
|
+
|
206
|
+
It is recommended that you define your own `Type` module and include it in your `ServiceBase` subclass, as so.
|
207
|
+
|
208
|
+
```rb
|
209
|
+
# app/models/types.rb
|
210
|
+
module Types
|
211
|
+
ApplicationRecord = Types.Instance(ApplicationRecord)
|
212
|
+
ControllerParams = ServiceBase::Types.Instance(ActionController::Parameters)
|
213
|
+
end
|
203
214
|
|
204
|
-
|
215
|
+
# app/services/application_service.rb
|
216
|
+
class ApplicationService < ServiceBase::Service
|
217
|
+
include Types
|
218
|
+
end
|
205
219
|
|
206
|
-
|
220
|
+
# app/services/example_service.rb
|
221
|
+
class ExampleService < ApplicationService
|
222
|
+
argument(:user, ApplicationRecord, description: "The user to update")
|
223
|
+
argument(:params, ControllerParams, description: "The attributes to update")
|
224
|
+
end
|
225
|
+
```
|
207
226
|
|
208
|
-
You can also limit the type of
|
227
|
+
You can also limit the type of `ApplicationRecord` record via
|
209
228
|
|
210
|
-
`argument(:
|
229
|
+
`argument(:user, Types.Instance(User))`
|
211
230
|
|
212
|
-
|
231
|
+
Or defining `User = Types.Instance(User)`
|
213
232
|
|
214
|
-
|
215
|
-
Types](https://dry-rb.org/gems/dry-types/1.2/built-in-types/). In order to access constants outside of the dry.rb namespace,
|
233
|
+
Note: In order to access constants outside of the dry.rb namespace,
|
216
234
|
or to access a type that collides with one of our defined types, you
|
217
235
|
must include `::` to allow a global constant search.
|
218
236
|
|
@@ -222,11 +240,9 @@ Ie. `::ApplicationRecord...`
|
|
222
240
|
powerful and recommended for automatic parsing of inputs, ie. controller
|
223
241
|
parameters.
|
224
242
|
|
225
|
-
For example `argument(:number, Params::Integer)` will
|
226
|
-
convert `"12"` ⇒ `12`
|
243
|
+
For example `argument(:number, Params::Integer)` will convert `"12"` ⇒ `12`.
|
227
244
|
|
228
|
-
Entire hash structures may also be validated and automatically
|
229
|
-
parsed, for example:
|
245
|
+
Entire hash structures may also be validated and automatically parsed, for example:
|
230
246
|
|
231
247
|
```ruby
|
232
248
|
argument(
|
@@ -7,10 +7,6 @@ module ServiceBase
|
|
7
7
|
if !klass.is_a?(Class) || !klass.ancestors.include?(Dry::Struct)
|
8
8
|
raise(TypeError, "#{name} should be extended on a Dry::Struct subclass")
|
9
9
|
end
|
10
|
-
|
11
|
-
# `Types` overrides default types to help shorthand Type::String.
|
12
|
-
# To access Ruby's native types within a service, use `::`, ie. `::String`
|
13
|
-
klass.include(Types)
|
14
10
|
end
|
15
11
|
|
16
12
|
def included(klass)
|
data/lib/service_base/service.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'dry-matcher'
|
4
|
-
require 'dry-struct'
|
5
|
-
require 'dry/matcher/result_matcher'
|
6
3
|
require 'dry/monads'
|
7
4
|
require 'dry/monads/do'
|
5
|
+
require 'dry/matcher/result_matcher'
|
6
|
+
require 'dry-types'
|
7
|
+
require 'dry-struct'
|
8
|
+
require 'dry-matcher'
|
8
9
|
require 'memery'
|
9
10
|
|
10
11
|
module ServiceBase
|
@@ -12,6 +13,7 @@ module ServiceBase
|
|
12
13
|
extend Dry::Monads::Result::Mixin::Constructors
|
13
14
|
include Dry::Monads::Do.for(:call)
|
14
15
|
include Dry::Monads[:result, :do]
|
16
|
+
include Dry.Types()
|
15
17
|
|
16
18
|
extend ArgumentTypeAnnotations
|
17
19
|
include Memery
|
@@ -95,24 +97,16 @@ module ServiceBase
|
|
95
97
|
end
|
96
98
|
end
|
97
99
|
|
100
|
+
# Returns a hash of all arguments and their values
|
101
|
+
def arguments
|
102
|
+
attributes
|
103
|
+
end
|
104
|
+
|
98
105
|
private
|
99
106
|
|
100
107
|
# The call method that must be defined by every inheriting service class
|
101
108
|
def call
|
102
109
|
raise(NotImplementedError)
|
103
110
|
end
|
104
|
-
|
105
|
-
# A locale lookup helper that uses the name of the service
|
106
|
-
def locale(selector, args = {})
|
107
|
-
class_name = self.class.name.gsub('::', '.').underscore
|
108
|
-
I18n.t(".#{selector}", scope: "services.#{class_name}", **args)
|
109
|
-
end
|
110
|
-
|
111
|
-
# Structured Monad Result Failure type for returning a ResponseError
|
112
|
-
class ResponseFailure < Dry::Monads::Result::Failure
|
113
|
-
def initialize(message, code, trace = Dry::Monads::RightBiased::Left.trace_caller)
|
114
|
-
super(ResponseError.new(message: message, code: code), trace)
|
115
|
-
end
|
116
|
-
end
|
117
111
|
end
|
118
112
|
end
|
data/lib/service_base/version.rb
CHANGED
data/lib/service_base.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: service_base
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Klein
|
@@ -94,7 +94,6 @@ files:
|
|
94
94
|
- lib/service_base/rspec.rb
|
95
95
|
- lib/service_base/rspec/service_support.rb
|
96
96
|
- lib/service_base/service.rb
|
97
|
-
- lib/service_base/types.rb
|
98
97
|
- lib/service_base/version.rb
|
99
98
|
homepage: https://github.com/kleinjm/service_base
|
100
99
|
licenses:
|
data/lib/service_base/types.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Defines Dry Types. These Types are included in the ServiceBase for type
|
4
|
-
# enforcement when defining `argument`s.
|
5
|
-
#
|
6
|
-
# For example, you may want to add `ApplicationRecord = Types.Instance(ApplicationRecord)`
|
7
|
-
#
|
8
|
-
# Add custom types as outlined in
|
9
|
-
# https://dry-rb.org/gems/dry-types/1.2/custom-types/
|
10
|
-
|
11
|
-
require 'dry-types'
|
12
|
-
|
13
|
-
module ServiceBase
|
14
|
-
module Types
|
15
|
-
include Dry.Types()
|
16
|
-
|
17
|
-
UpCasedString = Types::String.constructor(&:upcase)
|
18
|
-
Boolean = Bool # alias the built in type, Bool
|
19
|
-
end
|
20
|
-
end
|