state_machines 0.2.2 → 0.3.0
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 +4 -4
- data/Changelog.md +2 -0
- data/README.md +1 -2
- data/lib/state_machines/machine_collection.rb +23 -12
- data/lib/state_machines/transition.rb +1 -1
- data/lib/state_machines/transition_collection.rb +6 -6
- data/lib/state_machines/version.rb +1 -1
- data/test/unit/node_collection/{node_collection_after_being_copied_test/n.rb → node_collection_after_being_copied_test.rb} +0 -0
- data/test/unit/node_collection/{node_collection_after_update_test/n.rb → node_collection_after_update_test.rb} +0 -0
- data/test/unit/node_collection/{node_collection_with_indices_test/n.rb → node_collection_with_indices_test.rb} +0 -0
- data/test/unit/node_collection/{node_collection_with_nodes_test/n.rb → node_collection_with_nodes_test.rb} +0 -0
- data/test/unit/node_collection/{node_collection_with_numeric_index_test/n.rb → node_collection_with_numeric_index_test.rb} +0 -0
- data/test/unit/node_collection/{node_collection_with_postdefined_contexts_test/n.rb → node_collection_with_postdefined_contexts_test.rb} +0 -0
- data/test/unit/node_collection/{node_collection_with_predefined_contexts_test/n.rb → node_collection_with_predefined_contexts_test.rb} +0 -0
- data/test/unit/node_collection/{node_collection_with_string_index_test/n.rb → node_collection_with_string_index_test.rb} +0 -0
- data/test/unit/node_collection/{node_collection_with_symbol_index_test/n.rb → node_collection_with_symbol_index_test.rb} +0 -0
- data/test/unit/node_collection/{node_collection_without_indices_test/n.rb → node_collection_without_indices_test.rb} +0 -0
- data/test/unit/transition_collection/attribute_transition_collection_by_default_test.rb +1 -1
- data/test/unit/transition_collection/transition_collection_by_default_test.rb +1 -1
- data/test/unit/transition_collection/transition_collection_test.rb +1 -1
- data/test/unit/transition_collection/transition_collection_with_transactions_test.rb +1 -1
- data/test/unit/transition_collection/transition_collection_without_transactions_test.rb +1 -1
- metadata +22 -62
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dfbc8746b1a9da7b7103d3f4aeb4aac4f9774874
|
4
|
+
data.tar.gz: 7fea0a568c143c925769dde22c1259bc909f8bb8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a38794da50af36c33211543369062c8c8bdc3675617bbf4a2fa348da9e462315c30da692d06831840db515d45c42542e29d9428251fba9b5ce67270f6a30938
|
7
|
+
data.tar.gz: c6bf5cba1b08470e6533ce4f32a6a9eac23decb8d915c303b7f3a2f349578d58357f9c8a063df040ec770858cd0cfddd5ac8c6826788d794254a94555e59dd2e
|
data/Changelog.md
CHANGED
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
|
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
|
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 :
|
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, :
|
33
|
-
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
|
-
@
|
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
|
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, {:
|
195
|
+
super(transitions, {use_transactions: false, :actions => false}.merge(options))
|
196
196
|
end
|
197
197
|
|
198
198
|
private
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -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, :
|
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
|
-
],
|
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
|
-
],
|
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.
|
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-
|
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
|
-
|
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
|
-
|
371
|
-
|
372
|
-
-
|
373
|
-
|
374
|
-
|
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
|
-
|
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
|
-
|
834
|
-
|
835
|
-
-
|
836
|
-
|
837
|
-
|
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
|