piper-rb 0.4.0 → 0.4.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 187f1b8d52a186cffd6c89657c0521ef8f6c6e645b6d37d522c49ef4c89a5e8e
4
- data.tar.gz: a33dcc6e4900a897d6b35bf9b4f11e1d0076e622d62568b4b5455d639c392ea8
3
+ metadata.gz: 5b0d0ee76dc848c05fa9836adee64daaf9bc8d7397e7da2124d666b00b5af36f
4
+ data.tar.gz: 2e24b4e3d12b005b74a7da2babe135df101cd6c29fe698b97c1c2f5f1e7e53c3
5
5
  SHA512:
6
- metadata.gz: e564290a072a282ac5cef63e42b1fb9449968ecab94f72d6ad9112bba9c8c1e44295703a09e96679b36642ee60632e44385f24b1760470b74a41d6672907a453
7
- data.tar.gz: be8b7f693a219e707a4f092b7d2dfff7cd7ef8288f52ea506c0a376d75a78cc0a07bde32f3a7a1796a85b5c57803f9394bbe82211614e3f4acdda7d1ec8a329c
6
+ metadata.gz: 5b6bf69c81b2bc7f347bdb4082a0ed06faee2e207d13cd366e3d46e89c4a76754a52b284469ea33a2f338bec4241157420bf87ca00ea79769e30a08f803a7e0b
7
+ data.tar.gz: 0b1a5106dd21e07816772c7309c46c76cd5ded08845522483503563b0dfdc25cf5ab3c1e4800602c57cf8345f2905e651ca46ac47828b43cc822043c01ce471d
data/README.md CHANGED
@@ -10,7 +10,7 @@ gem "piper-rb"
10
10
 
11
11
  ## Usage
12
12
 
13
- ### super basic
13
+ ### Super basic
14
14
 
15
15
  The most basic element of `Piper` is a `pipe`.
16
16
  Just think of it as a condition step. If no keywords are used, then (in accordance to Ruby
@@ -47,7 +47,7 @@ YourSuperbService.new(input: "asd").call
47
47
 
48
48
  ```
49
49
 
50
- ### keywords for more control
50
+ ### Keywords for more control
51
51
 
52
52
  If you want to specify the condition, object to be returned and the error message, there are
53
53
  keywords for this:
@@ -90,7 +90,7 @@ YourFineService.new(input: "asd").call
90
90
 
91
91
  ```
92
92
 
93
- ### you can access last step's object with `last_result`:
93
+ ### You can access last step's object with `last_result`:
94
94
 
95
95
  ```ruby
96
96
  require "piper-rb"
@@ -112,7 +112,7 @@ YourGreatService.new(input: 20).call
112
112
  #=> Success(600)
113
113
  ```
114
114
 
115
- ### you can nest services, calling them in a pipe passes their result:
115
+ ### You can nest services, calling them in a pipe passes their result:
116
116
 
117
117
  ```ruby
118
118
  require "piper-rb"
@@ -145,3 +145,25 @@ YourMajesticService.new(input: 20).call
145
145
  YourMajesticService.new(input: 2).call
146
146
  #=> Failure({:service=>#<YourFlamboyantService nested_input=40>, :object=>false, :message=>"I am the one who knocks!"})
147
147
  ```
148
+
149
+ ---
150
+
151
+ ### You can configure default `nil` behavior.
152
+
153
+ By default, pipes fail when they end in `nil` and do not handle exceptions.
154
+ You can, of course, explicitly return `true` in order to make sure a pipe is successful, even if it ended in `nil`.
155
+
156
+
157
+ But in 0.4 you now have an option to tweak the default behavior, by using `pass_nil`. Bear in mind this option is inherited, so if your services inherit from, say, `BaseService`, then this - unless overwritten in a given service – will affect all services:
158
+
159
+ Refer to [specs](https://github.com/ellmo/piper-rb/blob/master/spec/service/configured_service_spec.rb) for more info.
160
+
161
+ ```ruby
162
+ require "piper-rb"
163
+
164
+ class BaseService < PiperService
165
+ pass_nil true
166
+
167
+ # [...]
168
+ end
169
+ ```
@@ -1,7 +1,7 @@
1
1
  module PiperDSL
2
2
  module PiperConfig
3
- DEFAULT__PASS_NIL = false
4
- DEFAULT__PASS_EXCEPTION = false
3
+ DEFAULT__PASS_NIL = false
4
+ DEFAULT__HANDLE_EXCEPTION = false
5
5
 
6
6
  def self.included(base)
7
7
  base.extend(ClassMethods)
@@ -22,18 +22,26 @@ module PiperDSL
22
22
  end
23
23
  end
24
24
 
25
- def pass_exception(val)
26
- @__pass_exception = val
25
+ def handle_exception(val)
26
+ @__handle_exception = val
27
27
  end
28
28
 
29
- def pass_exception?
30
- @__pass_exception ||= if defined?(@__pass_exception)
31
- @__pass_exception
32
- elsif superclass < PiperService
33
- superclass.pass_exception?
34
- else
35
- DEFAULT__PASS_EXCEPTION
36
- end
29
+ def handle_exception?
30
+ @__handle_exception ||= if defined?(@__handle_exception)
31
+ @__handle_exception
32
+ elsif superclass < PiperService
33
+ superclass.handle_exception?
34
+ else
35
+ DEFAULT__HANDLE_EXCEPTION
36
+ end
37
+ end
38
+
39
+ def skip_transaction!
40
+ @__skip_transaction = true
41
+ end
42
+
43
+ def skip_transaction?
44
+ !@__skip_transaction.nil?
37
45
  end
38
46
 
39
47
  def debug_steps
@@ -49,8 +57,12 @@ module PiperDSL
49
57
  self.class.pass_nil?
50
58
  end
51
59
 
52
- def pass_exception?
53
- self.class.pass_exception?
60
+ def handle_exception?
61
+ self.class.handle_exception?
62
+ end
63
+
64
+ def skip_transaction?
65
+ self.class.skip_transaction?
54
66
  end
55
67
 
56
68
  def debug_steps?
@@ -4,9 +4,11 @@ module PiperDSL
4
4
 
5
5
  attr_reader :service
6
6
 
7
- def initialize(step_name, &block)
8
- @step_name = step_name
9
- @block = block
7
+ def initialize(step_name, **options, &block)
8
+ @step_name = step_name
9
+ @block = block
10
+ @pass_nil = options[:pass_nil]
11
+ @handle_exception = options[:handle_exception]
10
12
  end
11
13
 
12
14
  def perform(service, last_result = nil)
@@ -30,7 +32,7 @@ module PiperDSL
30
32
  def prepare_response!(condition)
31
33
  if condition
32
34
  Success(@result_object || true)
33
- elsif condition.nil? && service.pass_nil?
35
+ elsif condition.nil? && pass_nil?
34
36
  Success(nil)
35
37
  else
36
38
  failure_object = { service: service, object: @fail_object, message: service.message }
@@ -41,9 +43,17 @@ module PiperDSL
41
43
  end
42
44
 
43
45
  def pass_exception!(exception)
44
- raise exception unless service.pass_exception?
46
+ raise exception unless handle_exception?
45
47
 
46
48
  Failure(service: service, object: exception, message: exception.message)
47
49
  end
50
+
51
+ def pass_nil?
52
+ @pass_nil.nil? ? service.pass_nil? : @pass_nil
53
+ end
54
+
55
+ def handle_exception?
56
+ @handle_exception.nil? ? service.handle_exception? : @handle_exception
57
+ end
48
58
  end
49
59
  end
@@ -26,7 +26,7 @@ class PiperService < Dry::Struct
26
26
  def call
27
27
  result = nil
28
28
 
29
- if defined? ActiveRecord::Base
29
+ if defined?(ActiveRecord::Base) && !skip_transaction?
30
30
  ActiveRecord::Base.transaction do
31
31
  result = perform_steps
32
32
 
@@ -39,10 +39,10 @@ class PiperService < Dry::Struct
39
39
  result
40
40
  end
41
41
 
42
- def self.pipe(desc, &block)
42
+ def self.pipe(desc, **options, &block)
43
43
  raise ArgumentError, "missing block" unless block_given?
44
44
 
45
- pipepart = PiperDSL::Pipe.new(desc, &block)
45
+ pipepart = PiperDSL::Pipe.new(desc, options, &block)
46
46
 
47
47
  service_steps << pipepart
48
48
  end
data/lib/piper/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module PiperGem
2
- VERSION = "0.4.0".freeze
2
+ VERSION = "0.4.1".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: piper-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakub Żuchowski