polist 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/CHANGELOG.md +11 -2
- data/README.md +35 -1
- data/lib/polist.rb +1 -0
- data/lib/polist/service.rb +33 -0
- data/lib/polist/service/middleware.rb +20 -0
- data/lib/polist/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ec034ec38c89c8f5446e20a9513c12e9778e485123bbcd56fc088d11e73baf26
|
4
|
+
data.tar.gz: d52d341772a6e1a312198d3b6e5c0caac885a27c6dca12c02464729aca643a72
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f33e028799cfd4b9c2da407ba407a50c3f298bd51bc5841db63e677b4a38c3b589f1736fb2ca2aba3525111cc1c28b783607d086d5432f068af48cd2077a5b17
|
7
|
+
data.tar.gz: 063a1f4aad8ac26b9458a79d7abc808d1031a1a3730b84e3b6395858614bc3a8f6c125114de692ee936ca80f1cd20c8ea6bbb5b065d5b4a7085e9c5b0ba95c5c
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -2,11 +2,17 @@
|
|
2
2
|
|
3
3
|
## [Unreleased]
|
4
4
|
|
5
|
+
## [1.1.0] - 2019-02-03
|
6
|
+
|
7
|
+
### Added
|
8
|
+
|
9
|
+
- Middlewares support ([@nesaulov]) [#2]
|
10
|
+
|
5
11
|
## [1.0.0] - 2019-02-03
|
6
12
|
|
7
13
|
### Added
|
8
14
|
|
9
|
-
-
|
15
|
+
- `Polist::Struct` and `Polist::Builder` ([@tycooon]) [#3]
|
10
16
|
|
11
17
|
## [0.4.0] - 2017-10-31
|
12
18
|
|
@@ -14,8 +20,11 @@
|
|
14
20
|
|
15
21
|
[0.4.0]: https://github.com/umbrellio/polist/tree/v0.4.0
|
16
22
|
[1.0.0]: https://github.com/umbrellio/polist/compare/v0.4.0...v1.0.0
|
17
|
-
[
|
23
|
+
[1.0.0]: https://github.com/umbrellio/polist/compare/v1.0.0...v1.1.0
|
24
|
+
[Unreleased]: https://github.com/umbrellio/polist/compare/v1.1.0...HEAD
|
18
25
|
|
26
|
+
[@nesaulov]: https://github.com/nesaulov
|
19
27
|
[@tycooon]: https://github.com/tycooon
|
20
28
|
|
29
|
+
[#2]: https://github.com/umbrellio/polist/pull/2
|
21
30
|
[#3]: https://github.com/umbrellio/polist/pull/3
|
data/README.md
CHANGED
@@ -54,7 +54,7 @@ rescue Polist::Service::Failure => error
|
|
54
54
|
end
|
55
55
|
```
|
56
56
|
|
57
|
-
Note that `.run` and `.call` are just shortcuts for `MyService.new(...).run` and `MyService.new(...).call` with the only difference that they always return the service instance instead of the result of `#run` or `#call`. Unlike `#call` though, `#run` is not intended to be
|
57
|
+
Note that `.run` and `.call` are just shortcuts for `MyService.new(...).run` and `MyService.new(...).call` with the only difference that they always return the service instance instead of the result of `#run` or `#call`. Unlike `#call` though, `#run` is not intended to be overwritten in subclasses.
|
58
58
|
|
59
59
|
### Using Form objects
|
60
60
|
|
@@ -169,6 +169,40 @@ c.x # => 15
|
|
169
169
|
c.y # => nil
|
170
170
|
```
|
171
171
|
|
172
|
+
### Using Middlewares
|
173
|
+
|
174
|
+
If you have some common things to be done in more than one service, you can define a middleware and register it inside the said services.
|
175
|
+
Every middleware takes the service into it's constructor and executes `#call`. Thus every middleware has to implement `#call` method and has a `#service` attribute reader.
|
176
|
+
Middlewares delegate `#success!`, `#fail!`, `#error!`, `#form`, `#form_attributes` to the service class they are registered in.
|
177
|
+
Every middleware should be a subclass of `Polist::Service::Middleware`. Middlewares are run before the service itself is run.
|
178
|
+
|
179
|
+
To register a middleware one should use `.register_middleware` class method on a service. More than one middleware can be registered for one service.
|
180
|
+
|
181
|
+
For example:
|
182
|
+
```ruby
|
183
|
+
class MyMiddleware < Polist::Service::Middleware
|
184
|
+
def call
|
185
|
+
fail!(code: :not_cool) if service.fail_on_middleware?
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
class MyService < Polist::Service
|
190
|
+
register_middleware MyMiddleware
|
191
|
+
|
192
|
+
def call
|
193
|
+
success!(code: :cool)
|
194
|
+
end
|
195
|
+
|
196
|
+
def fail_on_middleware?
|
197
|
+
true
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
service = MyService.run
|
202
|
+
service.success? #=> false
|
203
|
+
service.response #=> { code: :not_cool }
|
204
|
+
```
|
205
|
+
|
172
206
|
## Contributing
|
173
207
|
|
174
208
|
Bug reports and pull requests are welcome on GitHub at https://github.com/umbrellio/polist.
|
data/lib/polist.rb
CHANGED
data/lib/polist/service.rb
CHANGED
@@ -20,10 +20,20 @@ module Polist
|
|
20
20
|
include ActiveModel::Validations
|
21
21
|
end
|
22
22
|
|
23
|
+
module MiddlewareCaller
|
24
|
+
def call
|
25
|
+
call_middlewares
|
26
|
+
super
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
MiddlewareError = Class.new(StandardError)
|
31
|
+
|
23
32
|
attr_accessor :params
|
24
33
|
|
25
34
|
def self.inherited(klass)
|
26
35
|
klass.const_set(:Failure, Class.new(klass::Failure))
|
36
|
+
klass.prepend MiddlewareCaller
|
27
37
|
end
|
28
38
|
|
29
39
|
def self.build(*args)
|
@@ -44,6 +54,23 @@ module Polist
|
|
44
54
|
end
|
45
55
|
end
|
46
56
|
|
57
|
+
def self.__polist_middlewares__
|
58
|
+
@__polist_middlewares__ ||= []
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.register_middleware(klass)
|
62
|
+
unless klass < Polist::Service::Middleware
|
63
|
+
raise MiddlewareError,
|
64
|
+
"Middleware #{klass} should be a subclass of Polist::Service::Middleware"
|
65
|
+
end
|
66
|
+
|
67
|
+
__polist_middlewares__ << klass
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.__clear_middlewares__
|
71
|
+
@__polist_middlewares__ = []
|
72
|
+
end
|
73
|
+
|
47
74
|
def initialize(params = {})
|
48
75
|
self.params = params
|
49
76
|
end
|
@@ -76,6 +103,12 @@ module Polist
|
|
76
103
|
|
77
104
|
private
|
78
105
|
|
106
|
+
def call_middlewares
|
107
|
+
self.class.__polist_middlewares__.each do |middleware|
|
108
|
+
middleware.new(self).call
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
79
112
|
def form
|
80
113
|
@form ||= self.class::Form.new(form_attributes.to_snake_keys)
|
81
114
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Polist::Service::Middleware
|
4
|
+
def initialize(service)
|
5
|
+
@service = service
|
6
|
+
end
|
7
|
+
|
8
|
+
# Should be implemented in subclasses
|
9
|
+
def call; end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
attr_reader :service
|
14
|
+
|
15
|
+
%i[fail! error! success! form form_attributes].each do |service_method|
|
16
|
+
define_method(service_method) do |*args|
|
17
|
+
service.send(service_method, *args)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/polist/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: polist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuri Smirnov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-02-
|
11
|
+
date: 2019-02-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -169,6 +169,7 @@ files:
|
|
169
169
|
- lib/polist.rb
|
170
170
|
- lib/polist/builder.rb
|
171
171
|
- lib/polist/service.rb
|
172
|
+
- lib/polist/service/middleware.rb
|
172
173
|
- lib/polist/struct.rb
|
173
174
|
- lib/polist/version.rb
|
174
175
|
- polist.gemspec
|
@@ -191,7 +192,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
191
192
|
- !ruby/object:Gem::Version
|
192
193
|
version: '0'
|
193
194
|
requirements: []
|
194
|
-
rubygems_version: 3.0.
|
195
|
+
rubygems_version: 3.0.2
|
195
196
|
signing_key:
|
196
197
|
specification_version: 4
|
197
198
|
summary: A gem for creating simple service classes and more.
|