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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4a3ccf0f8d1e783625a5f9b9c7f812e199fd965daba514a26e24dd17579d1324
4
- data.tar.gz: 00c5fd25949bcbadcd391f526ed744428e82d657454c173a3598c196ffa50df5
3
+ metadata.gz: ec034ec38c89c8f5446e20a9513c12e9778e485123bbcd56fc088d11e73baf26
4
+ data.tar.gz: d52d341772a6e1a312198d3b6e5c0caac885a27c6dca12c02464729aca643a72
5
5
  SHA512:
6
- metadata.gz: d037d246cbb437c7c33e58cc54f682ceaeb0a041d03e8d70f81cb8d994725d5295069dd53507af0f3e01f0d4e39356e911c578c3fd9217a53e6c127db60ac6b9
7
- data.tar.gz: 657941a65cf0e2d806967663d7fd32949c96e41d35da152a9e2a95218ec4ecf374605db9df7b3584a810a897bcb7d4db84eb755bd834d81c2a81c845a8284bb2
6
+ metadata.gz: f33e028799cfd4b9c2da407ba407a50c3f298bd51bc5841db63e677b4a38c3b589f1736fb2ca2aba3525111cc1c28b783607d086d5432f068af48cd2077a5b17
7
+ data.tar.gz: 063a1f4aad8ac26b9458a79d7abc808d1031a1a3730b84e3b6395858614bc3a8f6c125114de692ee936ca80f1cd20c8ea6bbb5b065d5b4a7085e9c5b0ba95c5c
@@ -4,3 +4,6 @@ inherit_gem:
4
4
  AllCops:
5
5
  DisplayCopNames: true
6
6
  TargetRubyVersion: 2.5
7
+
8
+ Naming/MemoizedInstanceVariableName:
9
+ Enabled: false
@@ -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
- - Add `Polist::Struct` and `Polist::Builder` ([@tycooon]) [#3]
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
- [Unreleased]: https://github.com/umbrellio/polist/compare/v1.0.0...HEAD
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 owerwritten in subclasses.
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.
@@ -2,5 +2,6 @@
2
2
 
3
3
  require "polist/builder"
4
4
  require "polist/service"
5
+ require "polist/service/middleware"
5
6
  require "polist/struct"
6
7
  require "polist/version"
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Polist
4
- VERSION = "1.0.0"
4
+ VERSION = "1.1.0"
5
5
  end
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.0.0
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-03 00:00:00.000000000 Z
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.1
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.