polist 1.1.1 → 1.2.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: 5a65368a827c99be9dbeca560968d9d924630435a62cc4b887c196d9c4ffcc4a
4
- data.tar.gz: 651d41439429735882ed8f53f316e94b384fb8f09b9a74c1e32f8353e3845526
3
+ metadata.gz: d41a9ea7c84cd858fc92d79160ea448ed78fd36cfcf164195a7afe9cc5dbd59e
4
+ data.tar.gz: 2d2e676fd05d9b57f3b3159955a473933e95500de4f4e80f63b708a1cdc04f83
5
5
  SHA512:
6
- metadata.gz: 946a3ff3e0d93822f90c6d27b0801588db165033454cf8d14b459c81b1f6ceef3313c5a528faf4bab3ebbec3037512bbabdfbdf3be6534a7ec9b61b459e66e4d
7
- data.tar.gz: 5e3d0453b3f9a9ea152250192b3627cb7daf0d5f9db2f4ee2ae50ae0ffc18b24fe1442edb7ce4f204bc285f2d3f6afdfb87a86182ef4033323776efb9167f8bb
6
+ metadata.gz: f447ab40d832e15136acbf2b312f2941d2d18b0c8a046f40a4a3e26a76aa4e8be4df313e30962c665e3cc0f755ed1989f3ea8c6df050d0a4c28555fffd457558
7
+ data.tar.gz: 7e02ad12d1e8a13669a9e657c8ed7a181d19707c85c754c227c2fb006ba84e2b411ed4fe226001e8aeb2a3fa2a0aaee198eba1ef04d0432752f2353a8ce0f9fa
@@ -2,6 +2,12 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [1.2.0] - 2019-03-15
6
+
7
+ ### Added
8
+
9
+ - Block support for `#call` and `#run` ([@VanyaZ158]) [#5]
10
+
5
11
  ## [1.1.1] - 2019-02-09
6
12
 
7
13
  ### Fixed
@@ -28,11 +34,14 @@
28
34
  [1.0.0]: https://github.com/umbrellio/polist/compare/v0.4.0...v1.0.0
29
35
  [1.1.0]: https://github.com/umbrellio/polist/compare/v1.0.0...v1.1.0
30
36
  [1.1.1]: https://github.com/umbrellio/polist/compare/v1.1.0...v1.1.1
31
- [Unreleased]: https://github.com/umbrellio/polist/compare/v1.1.1...HEAD
37
+ [1.2.0]: https://github.com/umbrellio/polist/compare/v1.1.1...v1.2.0
38
+ [Unreleased]: https://github.com/umbrellio/polist/compare/v1.2.0...HEAD
32
39
 
33
40
  [@nesaulov]: https://github.com/nesaulov
41
+ [@VanyaZ158]: https://github.com/VanyaZ158
34
42
  [@tycooon]: https://github.com/tycooon
35
43
 
36
44
  [#2]: https://github.com/umbrellio/polist/pull/2
37
45
  [#3]: https://github.com/umbrellio/polist/pull/3
38
46
  [#4]: https://github.com/umbrellio/polist/pull/4
47
+ [#5]: https://github.com/umbrellio/polist/pull/5
data/README.md CHANGED
@@ -56,6 +56,43 @@ end
56
56
 
57
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
+ ### Using blocks in #call and #run methods.
60
+
61
+ You can use yield in `#call`. And then call `::run` or `::call` class methods with block. For example, we have the class:
62
+ ```ruby
63
+ class BlockFun < Polist::Service
64
+ def call
65
+ success!(yield(1, 2))
66
+ end
67
+ end
68
+ ```
69
+
70
+ Then we can use it like this:
71
+ ```ruby
72
+ service = BlockFun.call { |a, b| a + b }
73
+
74
+ p service.response # => 3
75
+ ```
76
+
77
+ Behind the scenes it just catches passed block in class methods `::run` and `::call`, converts it to proc and then passes proc to instance method `#call` and `#run` by converting it back to block. So, for example, if you want to pass this block to private methods, you can write code like this:
78
+ ```ruby
79
+ class AnotherBlockFun < Polist::Service
80
+ def call(&block)
81
+ success!(block_caller(&block))
82
+ end
83
+
84
+ private
85
+
86
+ def block_caller
87
+ yield 1, 2
88
+ end
89
+ end
90
+
91
+ service = AnotherBlockFun.call { |a, b| a + b }
92
+
93
+ p service.response # => 3
94
+ ```
95
+
59
96
  ### Using Form objects
60
97
 
61
98
  Sometimes you want to use some kind of params parsing and/or validation, and you can do that with the help of `Polist::Service::Form` class. It uses [tainbox](https://github.com/enthrops/tainbox) gem under the hood.
@@ -192,10 +229,10 @@ class MyService < Polist::Service
192
229
  def call
193
230
  success!(code: :cool)
194
231
  end
195
-
232
+
196
233
  def fail_on_middleware?
197
234
  true
198
- end
235
+ end
199
236
  end
200
237
 
201
238
  service = MyService.run
@@ -45,12 +45,12 @@ module Polist
45
45
  new(*args)
46
46
  end
47
47
 
48
- def self.call(*args)
49
- build(*args).tap(&:call)
48
+ def self.call(*args, &block)
49
+ build(*args).tap { |service| service.call(&block) }
50
50
  end
51
51
 
52
- def self.run(*args)
53
- build(*args).tap(&:run)
52
+ def self.run(*args, &block)
53
+ build(*args).tap { |service| service.run(&block) }
54
54
  end
55
55
 
56
56
  def self.param(*names)
@@ -83,8 +83,8 @@ module Polist
83
83
  # Should be implemented in subclasses
84
84
  def call; end
85
85
 
86
- def run
87
- call
86
+ def run(&block)
87
+ call(&block)
88
88
  rescue self.class::Failure => error
89
89
  @response = error.response
90
90
  @failure = true
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Polist
4
- VERSION = "1.1.1"
4
+ VERSION = "1.2.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.1.1
4
+ version: 1.2.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-09 00:00:00.000000000 Z
11
+ date: 2019-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -192,7 +192,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
192
192
  - !ruby/object:Gem::Version
193
193
  version: '0'
194
194
  requirements: []
195
- rubygems_version: 3.0.2
195
+ rubygems_version: 3.0.3
196
196
  signing_key:
197
197
  specification_version: 4
198
198
  summary: A gem for creating simple service classes and more.