nxt_state_machine 0.1.1 → 0.1.2

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: 619dd3e81ce389fb16df746e5d940c48d48f6e2c30b2afb108a3a098aa687003
4
- data.tar.gz: ee68dba22f251671e019a15f3e36bdbbb04fca5a5649e65fb1d0a43cedb60dbf
3
+ metadata.gz: 48ef8c1448e9b199d064eb4d4040e7187ca141cc1e41d7a4abef5914c71cae59
4
+ data.tar.gz: 500f0d1098da66be2c4da535061f3198177f5d6f3d314716a7154a423e6c7745
5
5
  SHA512:
6
- metadata.gz: 74cdf937fa1653f1fea76c4cf6a0fb42625b97cf1c3a08dc3ffaef34b9571400c4fc759ee002be24158c58adfeab2b5ef437d2a750b924a10dd8c5af7bccf665
7
- data.tar.gz: e1af7824092bb95bfe95b88d71c05b3f8262da0a95364fde1c2a55d31da44dc96113ec31c46539757d96d4a3d112fe12b1e8ba3684bb6740fbdb77a49011004f
6
+ metadata.gz: 134d73c9bfd5f12c9379a78d30ba601c8062ee530a469e4c7febda231202d1a0e4ff7ca4db9b84843ea7ff19eebadcd3f537b439738f7fc847b26884906dfbc3
7
+ data.tar.gz: 885c3d0c4898bb897cb16dcbd9bc19a7897c80fc58535dc539358ce9621f58bc1e5a183d9258715358680e2e7d77785adb790f08f3b84c2aa6bf46457ede6c84
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nxt_state_machine (0.1.1)
4
+ nxt_state_machine (0.1.2)
5
5
  activesupport
6
6
  nxt_registry (~> 0.1.3)
7
7
 
data/README.md CHANGED
@@ -41,25 +41,32 @@ class ArticleWorkflow
41
41
  # First we setup the states
42
42
  state :draft, initial: true
43
43
  states :written, :submitted # define multiple states at the same time
44
- state :approved
44
+ state :approved
45
45
  state :published
46
- state :rejected
47
- state :deleted
46
+ state :rejected, negative: true # You can pass options to states that you can query in the transition
47
+ state :deleted, negative: true do # States can even have custom methods if options are not sufficient
48
+ def deleted_at
49
+ Time.current
50
+ end
51
+ end
52
+
48
53
 
49
54
  event :write do
50
55
  transition from: %i[draft written deleted], to: :written
51
56
  end
52
57
 
53
58
  event :submit do
54
- # When the block takes arguments (instead of only keyword arguments!!)
55
- # the transition is always passed in as the first argument!!!
56
- transition from: %i[written rejected deleted], to: :submitted do |transition|
59
+ # If you want transitions to take arguments, we recommend to use keyword arguments
60
+ # When the block takes arguments (instead of just keyword arguments) the first argument
61
+ # passed to the block will always be the transition!
62
+ transition from: %i[written rejected deleted], to: :submitted do |transition, *others|
57
63
  puts transition.from.enum
58
64
  puts transition.to.enum
59
65
  end
60
66
  end
61
67
 
62
68
  event :approve do
69
+ # use methods as callbacks with run:
63
70
  before_transition from: %i[written submitted deleted], to: :approved, run: :call_me_back
64
71
 
65
72
  transition from: %i[written submitted deleted], to: :approved do |headline:|
@@ -68,6 +75,7 @@ class ArticleWorkflow
68
75
 
69
76
  after_transition from: %i[written submitted deleted], to: :approved, run: :call_me_back
70
77
 
78
+ # use blocks with callbacks
71
79
  around_transition from: any_state, to: :approved do |block|
72
80
  # Note that around transition callbacks get passed a proc object that you have to call
73
81
  puts 'around transition enter'
@@ -176,7 +184,8 @@ state.next # will give you the next state in the order they have been registered
176
184
  state.previous # will give you the previously registered state
177
185
  state.first? # first registered state?
178
186
  state.last? # last registered state?
179
- state.index # gives you the index of the state in the registry (can also be overwritten by passing index as an option)
187
+ state.index # gives you the index of the state in the registry
188
+ # You can also set indexes manually by passing in indexes when defining states. Make sure they are in order!
180
189
  ```
181
190
 
182
191
  ### Events
@@ -19,9 +19,9 @@ module NxtStateMachine
19
19
  self
20
20
  end
21
21
 
22
- # NOTE: allowing call(*args, **opts) is dangerous when called with a hash as an argument!
23
- # It will automatically become the **opts which might not be what you want! Probably better
24
- # to introduce arguments: [], options: { } or something
22
+ # NOTE: Currentl we only allow arguments! Not keyword args or **options
23
+ # If we would allow **options and we would pass a hash as the only argument it would
24
+ # automatically be parsed as the options!
25
25
  def call(*args)
26
26
  ensure_context_not_missing
27
27
 
@@ -1 +1,3 @@
1
- Error = Class.new(StandardError)
1
+ module NxtStateMachine
2
+ Error = Class.new(StandardError)
3
+ end
@@ -0,0 +1,5 @@
1
+ module NxtStateMachine
2
+ module Errors
3
+ StateWithSameIndexAlreadyRegistered = Class.new(Error)
4
+ end
5
+ end
@@ -7,6 +7,8 @@ module NxtStateMachine
7
7
  @transitions = []
8
8
  @options = opts.with_indifferent_access
9
9
  @index = opts.fetch(:index)
10
+
11
+ ensure_index_not_occupied
10
12
  end
11
13
 
12
14
  attr_accessor :enum, :initial, :index, :transitions, :state_machine, :options
@@ -16,23 +18,34 @@ module NxtStateMachine
16
18
  end
17
19
 
18
20
  def previous
19
- previous_index = (index - 1) % state_machine.states.size
20
- key = state_machine.states.keys[previous_index]
21
- state_machine.states.resolve(key)
21
+ current_index = sorted_states.index { |state| state.index == index }
22
+ sorted_states[(current_index - 1) % sorted_states.size]
22
23
  end
23
24
 
24
25
  def next
25
- next_index = (index + 1) % state_machine.states.size
26
- key = state_machine.states.keys[next_index]
27
- state_machine.states.resolve(key)
26
+ current_index = sorted_states.index { |state| state.index == index }
27
+ sorted_states[(current_index + 1) % sorted_states.size]
28
28
  end
29
29
 
30
30
  def last?
31
- index == state_machine.states.size - 1
31
+ sorted_states.last.index == index
32
32
  end
33
33
 
34
34
  def first?
35
- index == 0
35
+ sorted_states.first.index == index
36
+ end
37
+
38
+ private
39
+
40
+ def sorted_states
41
+ state_machine.states.values.sort_by(&:index)
42
+ end
43
+
44
+ def ensure_index_not_occupied
45
+ state_with_same_index = state_machine.states.values.find { |state| state.index == index }
46
+ return unless state_with_same_index
47
+
48
+ raise StateWithSameIndexAlreadyRegistered, "The index #{index} is already occupied by state: #{state_with_same_index.enum}"
36
49
  end
37
50
  end
38
51
  end
@@ -18,21 +18,15 @@ module NxtStateMachine
18
18
  attr_accessor :initial_state
19
19
 
20
20
  def get_state_with(method = nil, &block)
21
- method_or_block = (method || block)
22
- @get_state_with ||= method_or_block ||
23
- raise_missing_configuration_error(:get_state_with)
21
+ @get_state_with ||= (method || block) || raise_missing_configuration_error(:get_state_with)
24
22
  end
25
23
 
26
24
  def set_state_with(method = nil, &block)
27
- method_or_block = (method || block)
28
- @set_state_with ||= method_or_block ||
29
- raise_missing_configuration_error(:set_state_with)
25
+ @set_state_with ||= (method || block) || raise_missing_configuration_error(:set_state_with)
30
26
  end
31
27
 
32
28
  def set_state_with!(method = nil, &block)
33
- method_or_block = (method || block)
34
- @set_state_with_bang ||= method_or_block ||
35
- raise_missing_configuration_error(:set_state_with!)
29
+ @set_state_with_bang ||= (method || block) || raise_missing_configuration_error(:set_state_with!)
36
30
  end
37
31
 
38
32
  def state(*names, **opts, &block)
@@ -1,3 +1,3 @@
1
1
  module NxtStateMachine
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
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.1
4
+ version: 0.1.2
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-01-07 00:00:00.000000000 Z
14
+ date: 2020-01-08 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport
@@ -157,6 +157,7 @@ files:
157
157
  - lib/nxt_state_machine/errors/invalid_callback_option.rb
158
158
  - lib/nxt_state_machine/errors/missing_configuration.rb
159
159
  - lib/nxt_state_machine/errors/state_already_registered.rb
160
+ - lib/nxt_state_machine/errors/state_with_same_index_already_registered.rb
160
161
  - lib/nxt_state_machine/errors/transition_already_registered.rb
161
162
  - lib/nxt_state_machine/errors/transition_halted.rb
162
163
  - lib/nxt_state_machine/errors/transition_not_defined.rb