nxt_state_machine 0.1.9 → 0.1.11

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: 6128e445380f2ed2b836b4c23ed58556efd2ff0728add08c2cbd45e97812e068
4
- data.tar.gz: 05c8861e99afefec3c654366050b225d7a93c10ff984e508cd14d6345b1d0ff3
3
+ metadata.gz: bc37e9d88ae62233544b548a5085fe81f67be0957f498ebf0490a191ac37af8d
4
+ data.tar.gz: b999a6f14950de8f1bcca65082ff6c7ff9d7e61d29aeb0323e65d126502a5cdc
5
5
  SHA512:
6
- metadata.gz: d3038d32da3a7bfa5a49b4537e7c6f163259ddd0f3d2b48a05bfa838021350f648d75bfc493125529c9e1374dc34aa2c9abe94325790fe4ba82a09f01e2afdc8
7
- data.tar.gz: 64178102f125d3b180f31b1a0bbebd766226b44351bbef906d907cc6339f2ff213c743c51b52238706827cfd597e410b828eb0824f8a4747f363c9b2b79a0ce9
6
+ metadata.gz: bd80fa569f48c50ab81bc1380e1683d4d646a128ed5bae016d46a4c8263411f0de0bdf39934a480fdf55d0849fc516201444f2e2eecdf75ee903f45df5373661
7
+ data.tar.gz: af346178ff3e9321a4592862babffe717aceeabde96aa625768bd163070c99bad477514a9b2d4aa3b16553ebf3886039beed0748fee67a60c7cc4f96ed66b1dd
data/.circleci/config.yml CHANGED
@@ -1,15 +1,13 @@
1
- # Ruby CircleCI 2.0 configuration file
2
- #
3
- # Check https://circleci.com/docs/2.0/language-ruby/ for more details
4
- #
5
- version: 2
1
+ version: 2.1
2
+
3
+ orbs:
4
+ ruby: circleci/ruby@2.0.1
5
+ coveralls: coveralls/coveralls@1.0.6
6
+
6
7
  jobs:
7
8
  build:
8
9
  docker:
9
- # specify the version you desire here
10
- - image: circleci/ruby:2.6.5-node
11
- environment:
12
- BUNDLER_VERSION: 2.0.2
10
+ - image: 'cimg/ruby:3.2.2-node'
13
11
 
14
12
  working_directory: ~/repo
15
13
 
@@ -17,46 +15,12 @@ jobs:
17
15
  - checkout
18
16
 
19
17
  - run:
20
- name: Install apt dependencies
18
+ name: Install apt dependencies (graphviz)
21
19
  command: |
22
20
  sudo apt update -q \
23
21
  && sudo apt upgrade -q \
24
22
  && sudo apt install -qq graphviz
25
- # Download and cache dependencies
26
- - restore_cache:
27
- keys:
28
- - v1-dependencies-{{ checksum "Gemfile.lock" }}
29
-
30
- - run: gem install bundler --version $BUNDLER_VERSION
31
-
32
- - run:
33
- name: install dependencies
34
- command: |
35
- bundle install --jobs=4 --retry=3 --path vendor/bundle
36
-
37
- - save_cache:
38
- paths:
39
- - ./vendor/bundle
40
- key: v1-dependencies-{{ checksum "Gemfile.lock" }}
41
-
42
- # run tests!
43
- - run:
44
- name: run tests
45
- command: |
46
- mkdir /tmp/test-results
47
- TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | \
48
- circleci tests split --split-by=timings)"
49
-
50
- bundle exec rspec \
51
- --format progress \
52
- --format RspecJunitFormatter \
53
- --out /tmp/test-results/rspec.xml \
54
- --format progress \
55
- $TEST_FILES
56
-
57
- # collect reports
58
- - store_test_results:
59
- path: /tmp/test-results
60
- - store_artifacts:
61
- path: /tmp/test-results
62
- destination: test-results
23
+ - ruby/install-deps:
24
+ key: gems-v2
25
+ include-branch-in-cache-key: false
26
+ - ruby/rspec-test
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.6.5
1
+ 3.2.2
data/CHANGELOG.md CHANGED
@@ -1,7 +1,23 @@
1
- # v0.1.9 2020-09-23
1
+ # v0.1.11 2023-06-29
2
+
3
+ ### Added
4
+ - Added `state_machine.event_methods`
5
+
6
+ [Compare v0.1.10...v0.1.11](https://github.com/nxt-insurance/nxt_state_machine/compare/v0.1.10...v0.1.11)
7
+
8
+ # v0.1.10 2021-07-19
2
9
 
3
10
  ### Added
4
11
 
12
+ - Transition now optionally get passed the transition object that allows to access transition arguments and options.
13
+
14
+ [Compare v0.1.9...v0.1.10](https://github.com/nxt-insurance/nxt_state_machine/compare/v0.1.9...v0.1.10)
15
+
16
+
17
+ # v0.1.9 2020-09-23
18
+
19
+ ### Added
20
+
5
21
  - Allow to toggle locking of transitions for active record adapter per event or globally
6
22
 
7
23
  [Compare v0.1.8...v0.1.9](https://github.com/nxt-insurance/nxt_state_machine/compare/v0.1.8...v0.1.9)
data/Gemfile.lock CHANGED
@@ -1,60 +1,59 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nxt_state_machine (0.1.9)
4
+ nxt_state_machine (0.1.11)
5
5
  activesupport
6
6
  nxt_registry (~> 0.3.0)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- activemodel (6.0.3.3)
12
- activesupport (= 6.0.3.3)
13
- activerecord (6.0.3.3)
14
- activemodel (= 6.0.3.3)
15
- activesupport (= 6.0.3.3)
16
- activesupport (6.0.3.3)
11
+ activemodel (7.0.5.1)
12
+ activesupport (= 7.0.5.1)
13
+ activerecord (7.0.5.1)
14
+ activemodel (= 7.0.5.1)
15
+ activesupport (= 7.0.5.1)
16
+ activesupport (7.0.5.1)
17
17
  concurrent-ruby (~> 1.0, >= 1.0.2)
18
- i18n (>= 0.7, < 2)
19
- minitest (~> 5.1)
20
- tzinfo (~> 1.1)
21
- zeitwerk (~> 2.2, >= 2.2.2)
18
+ i18n (>= 1.6, < 2)
19
+ minitest (>= 5.1)
20
+ tzinfo (~> 2.0)
22
21
  coderay (1.1.3)
23
- concurrent-ruby (1.1.7)
24
- diff-lcs (1.4.4)
25
- i18n (1.8.5)
22
+ concurrent-ruby (1.2.2)
23
+ diff-lcs (1.5.0)
24
+ i18n (1.14.1)
26
25
  concurrent-ruby (~> 1.0)
27
26
  method_source (1.0.0)
28
- minitest (5.14.2)
29
- nxt_registry (0.3.2)
27
+ mini_portile2 (2.8.2)
28
+ minitest (5.18.1)
29
+ nxt_registry (0.3.10)
30
30
  activesupport
31
- pry (0.13.1)
31
+ pry (0.14.2)
32
32
  coderay (~> 1.1)
33
33
  method_source (~> 1.0)
34
34
  rake (12.3.3)
35
- rexml (3.2.4)
36
- rspec (3.9.0)
37
- rspec-core (~> 3.9.0)
38
- rspec-expectations (~> 3.9.0)
39
- rspec-mocks (~> 3.9.0)
40
- rspec-core (3.9.2)
41
- rspec-support (~> 3.9.3)
42
- rspec-expectations (3.9.2)
35
+ rexml (3.2.5)
36
+ rspec (3.12.0)
37
+ rspec-core (~> 3.12.0)
38
+ rspec-expectations (~> 3.12.0)
39
+ rspec-mocks (~> 3.12.0)
40
+ rspec-core (3.12.2)
41
+ rspec-support (~> 3.12.0)
42
+ rspec-expectations (3.12.3)
43
43
  diff-lcs (>= 1.2.0, < 2.0)
44
- rspec-support (~> 3.9.0)
45
- rspec-mocks (3.9.1)
44
+ rspec-support (~> 3.12.0)
45
+ rspec-mocks (3.12.5)
46
46
  diff-lcs (>= 1.2.0, < 2.0)
47
- rspec-support (~> 3.9.0)
48
- rspec-support (3.9.3)
49
- rspec_junit_formatter (0.4.1)
47
+ rspec-support (~> 3.12.0)
48
+ rspec-support (3.12.1)
49
+ rspec_junit_formatter (0.6.0)
50
50
  rspec-core (>= 2, < 4, != 2.12.0)
51
51
  ruby-graphviz (1.2.5)
52
52
  rexml
53
- sqlite3 (1.4.2)
54
- thread_safe (0.3.6)
55
- tzinfo (1.2.7)
56
- thread_safe (~> 0.1)
57
- zeitwerk (2.4.0)
53
+ sqlite3 (1.6.3)
54
+ mini_portile2 (~> 2.8.0)
55
+ tzinfo (2.0.6)
56
+ concurrent-ruby (~> 1.0)
58
57
 
59
58
  PLATFORMS
60
59
  ruby
data/README.md CHANGED
@@ -181,7 +181,14 @@ class Article < ApplicationRecord
181
181
  end
182
182
  ```
183
183
 
184
- You can also navigate between states
184
+ You can retrieve a list of states using the `states` method:
185
+
186
+ ```rb
187
+ states = Article.state_machine.states # returns a NxtStateMachine::StateRegistry instance
188
+ states.keys # ["draft", "written", "submitted", "approved", "published", "rejected", "deleted"]
189
+ ```
190
+
191
+ You can also navigate between states:
185
192
 
186
193
  ```ruby
187
194
  state.next # will give you the next state in the order they have been registered
@@ -192,6 +199,7 @@ state.index # gives you the index of the state in the registry
192
199
  # You can also set indexes manually by passing in indexes when defining states. Make sure they are in order!
193
200
  ```
194
201
 
202
+
195
203
  ### Events
196
204
 
197
205
  Once you have defined your states you can define events and their transitions. Events trigger state transitions based
@@ -241,15 +249,27 @@ article.approve(approved_at: Time.current)
241
249
  article.approve!(approved_at: Time.current)
242
250
  ```
243
251
 
244
- **NOTE:** Transitions run in transactions that acquire a lock to prevent concurrency issues per default.
245
- Transactions will be rolled back in case of an exception or if your target cannot be saved due to validation errors.
252
+ > **Note**:
253
+ >
254
+ > By default, transitions run in transactions that acquire a lock to prevent concurrency issues.
255
+ Transactions will be rolled back if an exception occurs or if your target cannot be saved due to validation errors.
246
256
  The state is set back to the state before the transition! If you try to transition on records with unpersisted changes
247
257
  you will get a `RuntimeError: Locking a record with unpersisted changes is not supported.` error saying something
248
258
  like `Use :save to persist the changes, or :reload to discard them explicitly.` since it's not possible to acquire a
249
- lock on modified records. You can also switch of locking and transactions for events by passing in the `lock_transitions: false`
259
+ lock on modified records.
260
+ >
261
+ > You can switch off locking and transactions for events by passing in the `lock_transitions: false`
250
262
  option when defining an event or globally on the state machine with the `lock_transitions: false` option. Currently
251
263
  there is no option to toggle locking at runtime.
252
264
 
265
+
266
+ You can retrieve a list of event methods with `event_methods`:
267
+
268
+ ```rb
269
+ Article.state_machine.event_methods
270
+ # => [:write, :submit, :approve, :publish, :reject, :delete, :write!, :submit!, :approve!, :publish!, :reject!, :delete!]
271
+ ```
272
+
253
273
  ### Transitions
254
274
 
255
275
  When your transition takes arguments other than keyword arguments, it will always be passed the transition object itself
@@ -298,7 +318,7 @@ event :approve do
298
318
 
299
319
  after_transition from: %i[written submitted deleted], to: :approved, run: :call_me_back
300
320
 
301
- around_transition from: any_state, to: :approved do |block|
321
+ around_transition from: any_state, to: :approved do |block, _transition|
302
322
  # Note that around transition callbacks get passed a proc object that you have to call
303
323
  puts 'around transition enter'
304
324
  block.call
@@ -312,6 +332,16 @@ event :approve do
312
332
  end
313
333
  ```
314
334
 
335
+ In callbacks you also have access to the current transition object. Through it you also have access to the arguments
336
+ and options that have been passed in when the transition was triggered:
337
+
338
+ ```ruby
339
+ before_transition from: any_state, to: :processed do |transition|
340
+ puts transition.arguments # => :arg_1, :arg_2 what was passed to the process!(:arg_1, :arg_2)
341
+ puts transition.options # => { arg_1: 'arg 1', arg_2: 'arg 2' } what was passed to the process!(arg_1: 'arg 1', arg_2: 'arg 2')
342
+ end
343
+ ```
344
+
315
345
  ### Error Callbacks
316
346
 
317
347
  You can also register callbacks that run in case of an error occurs. By defining the error class you can restrict
@@ -75,6 +75,13 @@ module NxtStateMachine
75
75
  states.values.map(&:enum)
76
76
  end
77
77
 
78
+ def event_methods
79
+ event_names = events.keys
80
+ event_names_with_bang = event_names.map { |e| e + '!' }
81
+
82
+ (event_names + event_names_with_bang).map(&:to_sym)
83
+ end
84
+
78
85
  alias_method :all_states, :any_state
79
86
 
80
87
  def all_states_except(*excluded)
@@ -25,7 +25,9 @@ module NxtStateMachine
25
25
  state_machine: state_machine,
26
26
  context: context,
27
27
  event: event,
28
- set_state_method: set_state_method
28
+ set_state_method: set_state_method,
29
+ arguments: args,
30
+ options: opts
29
31
  }
30
32
 
31
33
  transition = Transition.new(event.name, **options)
@@ -33,8 +35,12 @@ module NxtStateMachine
33
35
  if block
34
36
  # if the transition takes a block we make it available through a proxy on the transition itself!
35
37
  transition.send(:block=, Proc.new do
36
- # if the block takes arguments we always pass the transition as the first one
37
- args.prepend(transition) if block.arity > 0
38
+ # if the transition block takes arguments we always pass the transition itself as the first argument
39
+ # callbacks also get passed the transition object in case they take an argument and can access args and
40
+ # options passed to the transition when invoked through that transition object
41
+ if block.arity > 0
42
+ args = [transition] + args
43
+ end
38
44
  context.instance_exec(*args, **opts, &block)
39
45
  end)
40
46
  end
@@ -2,7 +2,7 @@ module NxtStateMachine
2
2
  class Transition
3
3
  include Transition::Interface
4
4
 
5
- def initialize(name, event:, from:, to:, state_machine:, context:, set_state_method:, &block)
5
+ def initialize(name, event:, from:, to:, state_machine:, context:, set_state_method:, arguments:, options:, &block)
6
6
  @name = name
7
7
  @event = event
8
8
  @from = state_machine.states.resolve!(from)
@@ -11,10 +11,12 @@ module NxtStateMachine
11
11
  @set_state_method = set_state_method
12
12
  @context = context
13
13
  @block = block
14
+ @arguments = arguments
15
+ @options = options
14
16
  @result = nil
15
17
  end
16
18
 
17
- attr_reader :name, :from, :to, :block, :event, :result
19
+ attr_reader :name, :from, :to, :block, :event, :result, :arguments, :options
18
20
 
19
21
  # This triggers the set state method
20
22
  def trigger
@@ -1,3 +1,3 @@
1
1
  module NxtStateMachine
2
- VERSION = '0.1.9'
2
+ VERSION = '0.1.11'
3
3
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nxt_state_machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.1.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Robecke
8
8
  - Nils Sommer
9
9
  - Raphael Kallensee
10
10
  - Lütfi Demirci
11
- autorequire:
11
+ autorequire:
12
12
  bindir: exe
13
13
  cert_chain: []
14
- date: 2020-09-30 00:00:00.000000000 Z
14
+ date: 2023-06-29 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport
@@ -152,7 +152,6 @@ files:
152
152
  - ".gitignore"
153
153
  - ".rspec"
154
154
  - ".ruby-version"
155
- - ".travis.yml"
156
155
  - CHANGELOG.md
157
156
  - Gemfile
158
157
  - Gemfile.lock
@@ -205,7 +204,7 @@ metadata:
205
204
  allowed_push_host: https://rubygems.org
206
205
  homepage_uri: https://github.com/nxt-insurance/nxt_state_machine
207
206
  source_code_uri: https://github.com/nxt-insurance/nxt_state_machine
208
- post_install_message:
207
+ post_install_message:
209
208
  rdoc_options: []
210
209
  require_paths:
211
210
  - lib
@@ -220,8 +219,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
220
219
  - !ruby/object:Gem::Version
221
220
  version: '0'
222
221
  requirements: []
223
- rubygems_version: 3.0.3
224
- signing_key:
222
+ rubygems_version: 3.4.10
223
+ signing_key:
225
224
  specification_version: 4
226
225
  summary: A rich but straight forward state machine library
227
226
  test_files: []
data/.travis.yml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- sudo: false
3
- language: ruby
4
- cache: bundler
5
- rvm:
6
- - 2.6.1
7
- before_install: gem install bundler -v 2.0.2