nxt_state_machine 0.1.6 → 0.1.7

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: 77b32ce5c0f8c968180650e6497e84076803948c832e84d5c63145ff10050c46
4
- data.tar.gz: 5a5f210aaf9b8f0d8278a66851f4d3be8f278a4a448975252610b3eebe292b06
3
+ metadata.gz: 37f01d32a03c0c09b47018719f8b05b2439497034807ad0e6fa098a6d7d832ed
4
+ data.tar.gz: '02682339637194f92d32240b759e51cab84cd590a789af74c66109e27d5ce912'
5
5
  SHA512:
6
- metadata.gz: fbbf6e38dc538a6e5018d9839c591ddf707cc5d348c310582680e8cf588715545214ebe1b16ab70dddad63a0cfa97ab7d66121752ffc1034813128fdb0f2e1e7
7
- data.tar.gz: ef95346d040eb48905820314241e67009d846c00c71725cc2ab24948db0e426d7cdddb1fd55c01d7e63a15d465d0342adf9d35f888d6c62d1d21ba596f19bd54
6
+ metadata.gz: e777bbfeffb013b7591d0d2e18169a7311d324aefe7ad2a97f181b53926f6d6aa0f2a3cd08728ca1e544ac21b5ca5893ae4479c39ff93e54ac083a0b8399cdb5
7
+ data.tar.gz: 53ff5dd521de00050a1fe553c7dfd560f1cc8fde10e8cfc1cdea571873b22f4ac3be103ec1863314b64da0a04d95244a19f655e41794395dbcc4b8708f720a8e
data/Gemfile.lock CHANGED
@@ -1,36 +1,36 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nxt_state_machine (0.1.6)
4
+ nxt_state_machine (0.1.7)
5
5
  activesupport
6
6
  nxt_registry (~> 0.1.3)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- activemodel (6.0.2.1)
12
- activesupport (= 6.0.2.1)
13
- activerecord (6.0.2.1)
14
- activemodel (= 6.0.2.1)
15
- activesupport (= 6.0.2.1)
16
- activesupport (6.0.2.1)
11
+ activemodel (6.0.2.2)
12
+ activesupport (= 6.0.2.2)
13
+ activerecord (6.0.2.2)
14
+ activemodel (= 6.0.2.2)
15
+ activesupport (= 6.0.2.2)
16
+ activesupport (6.0.2.2)
17
17
  concurrent-ruby (~> 1.0, >= 1.0.2)
18
18
  i18n (>= 0.7, < 2)
19
19
  minitest (~> 5.1)
20
20
  tzinfo (~> 1.1)
21
21
  zeitwerk (~> 2.2)
22
22
  coderay (1.1.2)
23
- concurrent-ruby (1.1.5)
23
+ concurrent-ruby (1.1.6)
24
24
  diff-lcs (1.3)
25
25
  i18n (1.8.2)
26
26
  concurrent-ruby (~> 1.0)
27
- method_source (0.9.2)
27
+ method_source (1.0.0)
28
28
  minitest (5.14.0)
29
- nxt_registry (0.1.4)
29
+ nxt_registry (0.1.5)
30
30
  activesupport
31
- pry (0.12.2)
32
- coderay (~> 1.1.0)
33
- method_source (~> 0.9.0)
31
+ pry (0.13.0)
32
+ coderay (~> 1.1)
33
+ method_source (~> 1.0)
34
34
  rake (12.3.3)
35
35
  rspec (3.9.0)
36
36
  rspec-core (~> 3.9.0)
@@ -51,7 +51,7 @@ GEM
51
51
  thread_safe (0.3.6)
52
52
  tzinfo (1.2.6)
53
53
  thread_safe (~> 0.1)
54
- zeitwerk (2.2.2)
54
+ zeitwerk (2.3.0)
55
55
 
56
56
  PLATFORMS
57
57
  ruby
data/README.md CHANGED
@@ -83,6 +83,10 @@ class ArticleWorkflow
83
83
  puts 'around transition exit'
84
84
  end
85
85
 
86
+ on_success from: any_state, to: :approved do |transition|
87
+ # This is the last callback in the chain - It runs outside of the active record transaction
88
+ end
89
+
86
90
  on_error CustomError from: any_state, to: :approved do |error, transition|
87
91
  end
88
92
  end
@@ -277,10 +281,10 @@ Transitions can be halted in callbacks and during the transition itself simply b
277
281
 
278
282
  ### Callbacks
279
283
 
280
- You can register `before_transition`, `around_transition` and `after_transition` callbacks. By defining the
281
- :from and :to states you decide on which transitions the callback actually runs. Around callbacks need to call the
282
- proc object that they get passed in. Registering callbacks inside an event block or on the state_machine top level
283
- behaves exactly the same way and is only a matter of structure. The only thing that defines when callbacks run is
284
+ You can register `before_transition`, `around_transition`, `after_transition` and `on_success` callbacks.
285
+ By defining the :from and :to states you decide on which transitions the callback actually runs. Around callbacks need
286
+ to call the proc object that they get passed in. Registering callbacks inside an event block or on the state_machine top
287
+ level behaves exactly the same way and is only a matter of structure. The only thing that defines when callbacks run is
284
288
  the :from and :to parameters with which they are registered.
285
289
 
286
290
 
@@ -298,6 +302,11 @@ event :approve do
298
302
  block.call
299
303
  puts 'around transition exit'
300
304
  end
305
+
306
+ # Use this to trigger another event after the transaction around the transition completed
307
+ on_success from: any_state, to: :approved do |transition|
308
+ # This is the last callback in the chain - It runs outside of the active record transaction
309
+ end
301
310
  end
302
311
  ```
303
312
 
@@ -18,6 +18,7 @@ module NxtStateMachine
18
18
  delegate :before_transition,
19
19
  :after_transition,
20
20
  :around_transition,
21
+ :on_success,
21
22
  :on_error,
22
23
  :on_error!,
23
24
  :any_state,
@@ -65,7 +65,7 @@ module NxtStateMachine
65
65
 
66
66
  raise defused_error if defused_error
67
67
 
68
- result
68
+ transition.run_success_callbacks || result
69
69
  rescue StandardError => error
70
70
  target.assign_attributes(state_attr => transition.from.to_s)
71
71
 
@@ -22,7 +22,7 @@ module NxtStateMachine
22
22
  result = set_state(target, transition, state_attr)
23
23
  transition.run_after_callbacks
24
24
 
25
- result
25
+ transition.run_success_callbacks || result
26
26
  rescue StandardError => error
27
27
  target.send("#{state_attr}=", transition.from.enum)
28
28
 
@@ -38,7 +38,7 @@ module NxtStateMachine
38
38
  result = set_state(target, transition, state_attr)
39
39
  transition.run_after_callbacks
40
40
 
41
- result
41
+ transition.run_success_callbacks || result
42
42
  rescue StandardError
43
43
  target.send("#{state_attr}=", transition.from.enum)
44
44
  raise
@@ -21,7 +21,7 @@ module NxtStateMachine
21
21
  transition.run_before_callbacks
22
22
  result = set_state(current_target, transition, state_attr)
23
23
  transition.run_after_callbacks
24
- result
24
+ transition.run_success_callbacks || result
25
25
  rescue StandardError => error
26
26
  current_target[state_attr] = transition.from.enum
27
27
 
@@ -37,7 +37,7 @@ module NxtStateMachine
37
37
  result = set_state(current_target, transition, state_attr)
38
38
  transition.run_after_callbacks
39
39
 
40
- result
40
+ transition.run_success_callbacks || result
41
41
  rescue StandardError
42
42
  current_target[state_attr] = transition.from.enum
43
43
  raise
@@ -117,6 +117,10 @@ module NxtStateMachine
117
117
  callbacks.register(from, to, :after, run, block)
118
118
  end
119
119
 
120
+ def on_success(from:, to:, run: nil, &block)
121
+ callbacks.register(from, to, :success, run, block)
122
+ end
123
+
120
124
  def defuse(errors = [], from:, to:)
121
125
  defuse_registry.register(from, to, errors)
122
126
  end
@@ -147,16 +151,23 @@ module NxtStateMachine
147
151
  run_callbacks(transition, :after, context)
148
152
  end
149
153
 
154
+ def run_success_callbacks(transition, context)
155
+ run_callbacks(transition, :success, context)
156
+ end
157
+
150
158
  def find_error_callback(error, transition)
151
159
  error_callback_registry.resolve(error, transition)
152
160
  end
153
161
 
154
162
  def run_callbacks(transition, kind, context)
155
163
  current_callbacks = callbacks.resolve(transition, kind)
164
+ return unless current_callbacks.any?
156
165
 
157
166
  current_callbacks.each do |callback|
158
167
  Callable.new(callback).bind(context).call(transition)
159
168
  end
169
+
170
+ true
160
171
  end
161
172
 
162
173
  def current_state_name(context)
@@ -11,9 +11,10 @@ module NxtStateMachine
11
11
  @set_state_method = set_state_method
12
12
  @context = context
13
13
  @block = block
14
+ @result = nil
14
15
  end
15
16
 
16
- attr_reader :name, :from, :to, :block, :event
17
+ attr_reader :name, :from, :to, :block, :event, :result
17
18
 
18
19
  # This triggers the set state method
19
20
  def trigger
@@ -31,7 +32,7 @@ module NxtStateMachine
31
32
 
32
33
  # This must be used in set_state method to actually execute the transition within the around callback chain
33
34
  def execute(&block)
34
- Transition::Proxy.new(event, state_machine,self, context).call(&block)
35
+ self.result = Transition::Proxy.new(event, state_machine,self, context).call(&block)
35
36
  end
36
37
 
37
38
  alias_method :with_around_callbacks, :execute
@@ -44,9 +45,13 @@ module NxtStateMachine
44
45
  state_machine.run_after_callbacks(self, context)
45
46
  end
46
47
 
48
+ def run_success_callbacks
49
+ state_machine.run_success_callbacks(self, context)
50
+ end
51
+
47
52
  private
48
53
 
49
54
  attr_reader :state_machine, :set_state_method, :context
50
- attr_writer :block
55
+ attr_writer :block, :result
51
56
  end
52
57
  end
@@ -1,3 +1,3 @@
1
1
  module NxtStateMachine
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nxt_state_machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Robecke
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: exe
13
13
  cert_chain: []
14
- date: 2020-03-02 00:00:00.000000000 Z
14
+ date: 2020-03-26 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport