state_machines 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (23) hide show
  1. checksums.yaml +4 -4
  2. data/Changelog.md +2 -0
  3. data/README.md +1 -2
  4. data/lib/state_machines/machine_collection.rb +23 -12
  5. data/lib/state_machines/transition.rb +1 -1
  6. data/lib/state_machines/transition_collection.rb +6 -6
  7. data/lib/state_machines/version.rb +1 -1
  8. data/test/unit/node_collection/{node_collection_after_being_copied_test/n.rb → node_collection_after_being_copied_test.rb} +0 -0
  9. data/test/unit/node_collection/{node_collection_after_update_test/n.rb → node_collection_after_update_test.rb} +0 -0
  10. data/test/unit/node_collection/{node_collection_with_indices_test/n.rb → node_collection_with_indices_test.rb} +0 -0
  11. data/test/unit/node_collection/{node_collection_with_nodes_test/n.rb → node_collection_with_nodes_test.rb} +0 -0
  12. data/test/unit/node_collection/{node_collection_with_numeric_index_test/n.rb → node_collection_with_numeric_index_test.rb} +0 -0
  13. data/test/unit/node_collection/{node_collection_with_postdefined_contexts_test/n.rb → node_collection_with_postdefined_contexts_test.rb} +0 -0
  14. data/test/unit/node_collection/{node_collection_with_predefined_contexts_test/n.rb → node_collection_with_predefined_contexts_test.rb} +0 -0
  15. data/test/unit/node_collection/{node_collection_with_string_index_test/n.rb → node_collection_with_string_index_test.rb} +0 -0
  16. data/test/unit/node_collection/{node_collection_with_symbol_index_test/n.rb → node_collection_with_symbol_index_test.rb} +0 -0
  17. data/test/unit/node_collection/{node_collection_without_indices_test/n.rb → node_collection_without_indices_test.rb} +0 -0
  18. data/test/unit/transition_collection/attribute_transition_collection_by_default_test.rb +1 -1
  19. data/test/unit/transition_collection/transition_collection_by_default_test.rb +1 -1
  20. data/test/unit/transition_collection/transition_collection_test.rb +1 -1
  21. data/test/unit/transition_collection/transition_collection_with_transactions_test.rb +1 -1
  22. data/test/unit/transition_collection/transition_collection_without_transactions_test.rb +1 -1
  23. metadata +22 -62
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 592765d98fcb41a672f7aab26d298b350095f2f1
4
- data.tar.gz: f5b1d1c24d1d6fef21593c9cccd1a284999f13f5
3
+ metadata.gz: dfbc8746b1a9da7b7103d3f4aeb4aac4f9774874
4
+ data.tar.gz: 7fea0a568c143c925769dde22c1259bc909f8bb8
5
5
  SHA512:
6
- metadata.gz: 6c2fca5379dd4adeac3f69560bacb568117ed3a41c0d65bba0c716c25655cf3a05b958d73db464479e16ecfd9ee4ab269ad37808a74551a8b5e86332a1557154
7
- data.tar.gz: 6ec09db4711dad2e998e87ce9d5a645e26429d498c50c5a739ae6911b1ce9e2b23a3176962226ef92d40d19aaf2bad5734f4d3ec82d7a8709a3275ec2774f280
6
+ metadata.gz: 3a38794da50af36c33211543369062c8c8bdc3675617bbf4a2fa348da9e462315c30da692d06831840db515d45c42542e29d9428251fba9b5ce67270f6a30938
7
+ data.tar.gz: c6bf5cba1b08470e6533ce4f32a6a9eac23decb8d915c303b7f3a2f349578d58357f9c8a063df040ec770858cd0cfddd5ac8c6826788d794254a94555e59dd2e
@@ -1,3 +1,5 @@
1
+ * Fixed inconsistent use of :use_transactions
2
+
1
3
  * Namespaced integrations are not registered by default anymore
2
4
 
3
5
  * Pass `static: false` in case you don't want initial states to be forced. e.g.
data/README.md CHANGED
@@ -39,7 +39,7 @@ Class definition:
39
39
  class Vehicle
40
40
  attr_accessor :seatbelt_on, :time_used, :auto_shop_busy
41
41
 
42
- state_machine state, initial: :parked do
42
+ state_machine :state, initial: :parked do
43
43
  before_transition parked: :any - :parked, do: :put_on_seatbelt
44
44
 
45
45
  after_transition on: :crash, do: :tow
@@ -591,7 +591,6 @@ For documenting state machines:
591
591
  ## TODO
592
592
 
593
593
  * Add matchers/assertions for rspec and minitest
594
- * Fix integrations dependency
595
594
 
596
595
  ## Contributing
597
596
 
@@ -25,50 +25,49 @@ module StateMachines
25
25
 
26
26
  result = yield if block_given?
27
27
 
28
- each_value do |machine|
28
+ each_value do |machine|
29
29
  unless machine.dynamic_initial_state?
30
30
  force = options[:static] == :force || !attributes.keys.map(&:to_sym).include?(machine.attribute)
31
31
  machine.initialize_state(object, force: force, :to => options[:to])
32
32
  end
33
33
  end if options[:static]
34
-
34
+
35
35
  each_value do |machine|
36
36
  machine.initialize_state(object, :force => options[:dynamic] == :force, :to => options[:to]) if machine.dynamic_initial_state?
37
37
  end if options[:dynamic]
38
38
 
39
39
  result
40
40
  end
41
-
41
+
42
42
  # Runs one or more events in parallel on the given object. See
43
43
  # StateMachines::InstanceMethods#fire_events for more information.
44
44
  def fire_events(object, *events)
45
45
  run_action = [true, false].include?(events.last) ? events.pop : true
46
-
46
+
47
47
  # Generate the transitions to run for each event
48
48
  transitions = events.collect do |event_name|
49
49
  # Find the actual event being run
50
50
  event = nil
51
51
  detect {|name, machine| event = machine.events[event_name, :qualified_name]}
52
-
52
+
53
53
  raise(InvalidEvent.new(object, event_name)) unless event
54
-
54
+
55
55
  # Get the transition that will be performed for the event
56
56
  unless transition = event.transition_for(object)
57
57
  event.on_failure(object)
58
58
  end
59
-
60
59
  transition
61
60
  end.compact
62
-
61
+
63
62
  # Run the events in parallel only if valid transitions were found for
64
63
  # all of them
65
64
  if events.length == transitions.length
66
- TransitionCollection.new(transitions, :actions => run_action).perform
65
+ TransitionCollection.new(transitions, {use_transactions: resolve_use_transactions, actions: run_action}).perform
67
66
  else
68
67
  false
69
68
  end
70
69
  end
71
-
70
+
72
71
  # Builds the collection of transitions for all event attributes defined on
73
72
  # the given object. This will only include events whose machine actions
74
73
  # match the one specified.
@@ -78,8 +77,20 @@ module StateMachines
78
77
  transitions = map do |name, machine|
79
78
  machine.events.attribute_transition_for(object, true) if machine.action == action
80
79
  end
81
-
82
- AttributeTransitionCollection.new(transitions.compact, options)
80
+
81
+ AttributeTransitionCollection.new(transitions.compact, {use_transactions: resolve_use_transactions}.merge(options))
82
+ end
83
+
84
+ protected
85
+
86
+ def resolve_use_transactions
87
+ use_transactions = nil
88
+ each_value do |machine|
89
+ # Determine use_transactions setting for this set of transitions. If from multiple state_machines, the settings must match.
90
+ raise 'Encountered mismatched use_transactions configurations for multiple state_machines' if !use_transactions.nil? && use_transactions != machine.use_transactions
91
+ use_transactions = machine.use_transactions
92
+ end
93
+ use_transactions
83
94
  end
84
95
  end
85
96
  end
@@ -160,7 +160,7 @@ module StateMachines
160
160
  self.args = args
161
161
 
162
162
  # Run the transition
163
- !!TransitionCollection.new([self], :actions => run_action).perform
163
+ !!TransitionCollection.new([self], {use_transactions: machine.use_transactions, actions: run_action}).perform
164
164
  end
165
165
 
166
166
  # Runs a block within a transaction for the object being transitioned.
@@ -10,7 +10,7 @@ module StateMachines
10
10
  attr_reader :skip_after
11
11
 
12
12
  # Whether transitions should wrapped around a transaction block
13
- attr_reader :use_transaction
13
+ attr_reader :use_transactions
14
14
 
15
15
  # Creates a new collection of transitions that can be run in parallel. Each
16
16
  # transition *must* be for a different attribute.
@@ -29,11 +29,11 @@ module StateMachines
29
29
  attributes = map {|transition| transition.attribute}.uniq
30
30
  fail ArgumentError, 'Cannot perform multiple transitions in parallel for the same state machine attribute' if attributes.length != length
31
31
 
32
- options.assert_valid_keys(:actions, :after, :transaction)
33
- options = {:actions => true, :after => true, :transaction => true}.merge(options)
32
+ options.assert_valid_keys(:actions, :after, :use_transactions)
33
+ options = {actions: true, after: true, use_transactions: true}.merge(options)
34
34
  @skip_actions = !options[:actions]
35
35
  @skip_after = !options[:after]
36
- @use_transaction = options[:transaction]
36
+ @use_transactions = options[:use_transactions]
37
37
  end
38
38
 
39
39
  # Runs each of the collection's transitions in parallel.
@@ -177,7 +177,7 @@ module StateMachines
177
177
  # Runs a block within a transaction for the object being transitioned. If
178
178
  # transactions are disabled, then this is a no-op.
179
179
  def within_transaction
180
- if use_transaction && !empty?
180
+ if use_transactions && !empty?
181
181
  first.within_transaction do
182
182
  yield
183
183
  success?
@@ -192,7 +192,7 @@ module StateMachines
192
192
  # based events
193
193
  class AttributeTransitionCollection < TransitionCollection
194
194
  def initialize(transitions = [], options = {}) #:nodoc:
195
- super(transitions, {:transaction => false, :actions => false}.merge(options))
195
+ super(transitions, {use_transactions: false, :actions => false}.merge(options))
196
196
  end
197
197
 
198
198
  private
@@ -1,3 +1,3 @@
1
1
  module StateMachines
2
- VERSION = '0.2.2'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -14,7 +14,7 @@ class AttributeTransitionCollectionByDefaultTest < StateMachinesTest
14
14
  end
15
15
 
16
16
  def test_should_not_use_transaction
17
- refute @transitions.use_transaction
17
+ refute @transitions.use_transactions
18
18
  end
19
19
 
20
20
  def test_should_be_empty
@@ -14,7 +14,7 @@ class TransitionCollectionByDefaultTest < StateMachinesTest
14
14
  end
15
15
 
16
16
  def test_should_use_transaction
17
- assert @transitions.use_transaction
17
+ assert @transitions.use_transactions
18
18
  end
19
19
 
20
20
  def test_should_be_empty
@@ -3,7 +3,7 @@ require_relative '../../test_helper'
3
3
  class TransitionCollectionTest < StateMachinesTest
4
4
  def test_should_raise_exception_if_invalid_option_specified
5
5
  exception = assert_raises(ArgumentError) { StateMachines::TransitionCollection.new([], invalid: true) }
6
- assert_equal 'Unknown key: :invalid. Valid keys are: :actions, :after, :transaction', exception.message
6
+ assert_equal 'Unknown key: :invalid. Valid keys are: :actions, :after, :use_transactions', exception.message
7
7
  end
8
8
 
9
9
  def test_should_raise_exception_if_multiple_transitions_for_same_attribute_specified
@@ -21,7 +21,7 @@ class TransitionCollectionWithTransactionsTest < StateMachinesTest
21
21
  @object = @klass.new
22
22
  @transitions = StateMachines::TransitionCollection.new([
23
23
  StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
24
- ], transaction: true)
24
+ ], use_transactions: true)
25
25
  end
26
26
 
27
27
  def test_should_run_before_callbacks_within_transaction
@@ -19,7 +19,7 @@ class TransitionCollectionWithoutTransactionsTest < StateMachinesTest
19
19
  @object = @klass.new
20
20
  @transitions = StateMachines::TransitionCollection.new([
21
21
  StateMachines::Transition.new(@object, @machine, :ignite, :parked, :idling)
22
- ], transaction: false)
22
+ ], use_transactions: false)
23
23
  @transitions.perform
24
24
  end
25
25
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_machines
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-02-06 00:00:00.000000000 Z
12
+ date: 2015-04-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -354,39 +354,19 @@ files:
354
354
  - test/unit/matcher_helpers/matcher_helpers_all_test.rb
355
355
  - test/unit/matcher_helpers/matcher_helpers_any_test.rb
356
356
  - test/unit/matcher_helpers/matcher_helpers_same_test.rb
357
- - |-
358
- test/unit/node_collection/node_collection_after_being_copied_test
359
- .rb
360
- - |-
361
- test/unit/node_collection/node_collection_after_update_test
362
- .rb
357
+ - test/unit/node_collection/node_collection_after_being_copied_test.rb
358
+ - test/unit/node_collection/node_collection_after_update_test.rb
363
359
  - test/unit/node_collection/node_collection_by_default_test.rb
364
360
  - test/unit/node_collection/node_collection_test.rb
365
- - |-
366
- test/unit/node_collection/node_collection_with_indices_test
367
- .rb
361
+ - test/unit/node_collection/node_collection_with_indices_test.rb
368
362
  - test/unit/node_collection/node_collection_with_matcher_contexts_test.rb
369
- - |-
370
- test/unit/node_collection/node_collection_with_nodes_test
371
- .rb
372
- - |-
373
- test/unit/node_collection/node_collection_with_numeric_index_test
374
- .rb
375
- - |-
376
- test/unit/node_collection/node_collection_with_postdefined_contexts_test
377
- .rb
378
- - |-
379
- test/unit/node_collection/node_collection_with_predefined_contexts_test
380
- .rb
381
- - |-
382
- test/unit/node_collection/node_collection_with_string_index_test
383
- .rb
384
- - |-
385
- test/unit/node_collection/node_collection_with_symbol_index_test
386
- .rb
387
- - |-
388
- test/unit/node_collection/node_collection_without_indices_test
389
- .rb
363
+ - test/unit/node_collection/node_collection_with_nodes_test.rb
364
+ - test/unit/node_collection/node_collection_with_numeric_index_test.rb
365
+ - test/unit/node_collection/node_collection_with_postdefined_contexts_test.rb
366
+ - test/unit/node_collection/node_collection_with_predefined_contexts_test.rb
367
+ - test/unit/node_collection/node_collection_with_string_index_test.rb
368
+ - test/unit/node_collection/node_collection_with_symbol_index_test.rb
369
+ - test/unit/node_collection/node_collection_without_indices_test.rb
390
370
  - test/unit/path/path_by_default_test.rb
391
371
  - test/unit/path/path_test.rb
392
372
  - test/unit/path/path_with_available_transitions_after_reaching_target_test.rb
@@ -817,39 +797,19 @@ test_files:
817
797
  - test/unit/matcher_helpers/matcher_helpers_all_test.rb
818
798
  - test/unit/matcher_helpers/matcher_helpers_any_test.rb
819
799
  - test/unit/matcher_helpers/matcher_helpers_same_test.rb
820
- - |-
821
- test/unit/node_collection/node_collection_after_being_copied_test
822
- .rb
823
- - |-
824
- test/unit/node_collection/node_collection_after_update_test
825
- .rb
800
+ - test/unit/node_collection/node_collection_after_being_copied_test.rb
801
+ - test/unit/node_collection/node_collection_after_update_test.rb
826
802
  - test/unit/node_collection/node_collection_by_default_test.rb
827
803
  - test/unit/node_collection/node_collection_test.rb
828
- - |-
829
- test/unit/node_collection/node_collection_with_indices_test
830
- .rb
804
+ - test/unit/node_collection/node_collection_with_indices_test.rb
831
805
  - test/unit/node_collection/node_collection_with_matcher_contexts_test.rb
832
- - |-
833
- test/unit/node_collection/node_collection_with_nodes_test
834
- .rb
835
- - |-
836
- test/unit/node_collection/node_collection_with_numeric_index_test
837
- .rb
838
- - |-
839
- test/unit/node_collection/node_collection_with_postdefined_contexts_test
840
- .rb
841
- - |-
842
- test/unit/node_collection/node_collection_with_predefined_contexts_test
843
- .rb
844
- - |-
845
- test/unit/node_collection/node_collection_with_string_index_test
846
- .rb
847
- - |-
848
- test/unit/node_collection/node_collection_with_symbol_index_test
849
- .rb
850
- - |-
851
- test/unit/node_collection/node_collection_without_indices_test
852
- .rb
806
+ - test/unit/node_collection/node_collection_with_nodes_test.rb
807
+ - test/unit/node_collection/node_collection_with_numeric_index_test.rb
808
+ - test/unit/node_collection/node_collection_with_postdefined_contexts_test.rb
809
+ - test/unit/node_collection/node_collection_with_predefined_contexts_test.rb
810
+ - test/unit/node_collection/node_collection_with_string_index_test.rb
811
+ - test/unit/node_collection/node_collection_with_symbol_index_test.rb
812
+ - test/unit/node_collection/node_collection_without_indices_test.rb
853
813
  - test/unit/path/path_by_default_test.rb
854
814
  - test/unit/path/path_test.rb
855
815
  - test/unit/path/path_with_available_transitions_after_reaching_target_test.rb