state_machines 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Changelog.md +12 -0
- data/Contributors.md +39 -0
- data/lib/state_machines/machine_collection.rb +8 -7
- data/lib/state_machines/version.rb +1 -1
- data/test/unit/machine/machine_with_static_initial_state_test.rb +2 -13
- data/test/unit/machine_collection/machine_collection_state_initialization_test.rb +9 -9
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3935f97b4e0314e076a6a35afc73f4f187058293
|
4
|
+
data.tar.gz: f2e5c9374a4f1b05729035f14f4909dc7f218518
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 48312c212788cc3b03356ea37dbfa1cd5b73ea881b2f61f6367d3d8ad3eece0c145b79bc47b7c895b222651f887af31ca3695028c2c8f87d43f4e7a166944d42
|
7
|
+
data.tar.gz: 4e05b6813f79d0374d9f21be4f91567fa09ed5c18fb9dd99ae108be5e1d813f149fbd757b3ee11ff1f9988d0819a3fa81907967cbcdbb0858d8a12c3de27514a
|
data/Changelog.md
CHANGED
@@ -0,0 +1,12 @@
|
|
1
|
+
* Pass `static: false` in case you don't want initial states to be forced. e.g.
|
2
|
+
|
3
|
+
```ruby
|
4
|
+
# will set the initial machine state
|
5
|
+
@machines.initialize_states(@object)
|
6
|
+
|
7
|
+
# optionally you can pass the attributes to have that as the initial state
|
8
|
+
@machines.initialize_states(@object, {}, { state: 'finished' })
|
9
|
+
|
10
|
+
# or pass set `static` to false if you want to keep the `object.state` current value
|
11
|
+
@machines.initialize_states(@object, { static: false })
|
12
|
+
```
|
data/Contributors.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
- Aaron Gibralter
|
2
|
+
- Aaron Pfeifer
|
3
|
+
- Abdelkader Boudih
|
4
|
+
- Akira Matsuda
|
5
|
+
- Andrea Longhi
|
6
|
+
- Brad Heller
|
7
|
+
- Brandon Dimcheff
|
8
|
+
- Casey Howard
|
9
|
+
- Chinasaur
|
10
|
+
- Daniel Huckstep
|
11
|
+
- Durran Jordan
|
12
|
+
- Gareth Adams
|
13
|
+
- Jahangir Zinedine
|
14
|
+
- Jeremy Wells
|
15
|
+
- Joe Lind
|
16
|
+
- Jon Evans
|
17
|
+
- Markus Schirp
|
18
|
+
- Michael Klishin
|
19
|
+
- Mikhail Shirkov
|
20
|
+
- Mohamed Alouane
|
21
|
+
- Nate Murray
|
22
|
+
- Nathan Long
|
23
|
+
- Nicolas Blanco
|
24
|
+
- Pawel Pierzchala
|
25
|
+
- Pete Forde
|
26
|
+
- Peter Lampesberger
|
27
|
+
- Rin Raeuber
|
28
|
+
- Robert Poor
|
29
|
+
- Rustam Zagirov
|
30
|
+
- Sandro Turriate and Tim Pope
|
31
|
+
- Sean O'Brien
|
32
|
+
- Stefan Penner
|
33
|
+
- Steve Richert
|
34
|
+
- Wojciech Wnętrzak
|
35
|
+
- @chris
|
36
|
+
- @gmitrev
|
37
|
+
- @nblumoe
|
38
|
+
- @reiner
|
39
|
+
- @sanemat
|
@@ -1,8 +1,6 @@
|
|
1
1
|
module StateMachines
|
2
2
|
# Represents a collection of state machines for a class
|
3
3
|
class MachineCollection < Hash
|
4
|
-
|
5
|
-
|
6
4
|
# Initializes the state of each machine in the given object. This can allow
|
7
5
|
# states to be initialized in two groups: static and dynamic. For example:
|
8
6
|
#
|
@@ -13,22 +11,25 @@ module StateMachines
|
|
13
11
|
# If no block is provided, then all states will still be initialized.
|
14
12
|
#
|
15
13
|
# Valid configuration options:
|
16
|
-
# * <tt>:static</tt> - Whether to initialize static states.
|
17
|
-
#
|
18
|
-
# Default is
|
14
|
+
# * <tt>:static</tt> - Whether to initialize static states. Unless set to
|
15
|
+
# false, the state will be initialized regardless of its current value.
|
16
|
+
# Default is true.
|
19
17
|
# * <tt>:dynamic</tt> - Whether to initialize dynamic states. If set to
|
20
18
|
# :force, the state will be initialized regardless of its current value.
|
21
19
|
# Default is true.
|
22
20
|
# * <tt>:to</tt> - A hash to write the initialized state to instead of
|
23
21
|
# writing to the object. Default is to write directly to the object.
|
24
|
-
def initialize_states(object, options = {})
|
22
|
+
def initialize_states(object, options = {}, attributes = {})
|
25
23
|
options.assert_valid_keys( :static, :dynamic, :to)
|
26
24
|
options = {:static => true, :dynamic => true}.merge(options)
|
27
25
|
|
28
26
|
result = yield if block_given?
|
29
27
|
|
30
28
|
each_value do |machine|
|
31
|
-
|
29
|
+
unless machine.dynamic_initial_state?
|
30
|
+
force = options[:static] == :force || !attributes.keys.include?(machine.attribute)
|
31
|
+
machine.initialize_state(object, force: force, :to => options[:to])
|
32
|
+
end
|
32
33
|
end if options[:static]
|
33
34
|
|
34
35
|
each_value do |machine|
|
@@ -29,18 +29,7 @@ class MachineWithStaticInitialStateTest < StateMachinesTest
|
|
29
29
|
assert_equal 'parked', @klass.new.state
|
30
30
|
end
|
31
31
|
|
32
|
-
def
|
33
|
-
@klass.class_eval do
|
34
|
-
def initialize(_attributes = {})
|
35
|
-
self.state = 'idling'
|
36
|
-
super()
|
37
|
-
end
|
38
|
-
end
|
39
|
-
object = @klass.new
|
40
|
-
assert_equal 'idling', object.state
|
41
|
-
end
|
42
|
-
|
43
|
-
def test_should_set_initial_state_prior_to_initialization
|
32
|
+
def test_should_not_initial_state_prior_to_initialization
|
44
33
|
base = Class.new do
|
45
34
|
attr_accessor :state_on_init
|
46
35
|
|
@@ -51,7 +40,7 @@ class MachineWithStaticInitialStateTest < StateMachinesTest
|
|
51
40
|
klass = Class.new(base)
|
52
41
|
StateMachines::Machine.new(klass, initial: :parked)
|
53
42
|
|
54
|
-
|
43
|
+
assert_nil klass.new.state_on_init
|
55
44
|
end
|
56
45
|
|
57
46
|
def test_should_be_included_in_known_states
|
@@ -7,8 +7,8 @@ class MachineCollectionStateInitializationTest < StateMachinesTest
|
|
7
7
|
@klass = Class.new
|
8
8
|
|
9
9
|
@machines[:state] = StateMachines::Machine.new(@klass, :state, initial: :parked)
|
10
|
-
@machines[:alarm_state] = StateMachines::Machine.new(@klass, :alarm_state, initial:
|
11
|
-
@machines[:alarm_state].state :active, value:
|
10
|
+
@machines[:alarm_state] = StateMachines::Machine.new(@klass, :alarm_state, initial: ->(_object) { :active })
|
11
|
+
@machines[:alarm_state].state :active, value: -> { 'active' }
|
12
12
|
|
13
13
|
# Prevent the auto-initialization hook from firing
|
14
14
|
@klass.class_eval do
|
@@ -25,17 +25,17 @@ class MachineCollectionStateInitializationTest < StateMachinesTest
|
|
25
25
|
assert_raises(ArgumentError) { @machines.initialize_states(@object, invalid: true) }
|
26
26
|
end
|
27
27
|
|
28
|
-
def
|
28
|
+
def test_should_initialize_static_states_after_block
|
29
29
|
@machines.initialize_states(@object) do
|
30
30
|
@state_in_block = @object.state
|
31
31
|
@alarm_state_in_block = @object.alarm_state
|
32
32
|
end
|
33
33
|
|
34
|
-
|
34
|
+
assert_nil @state_in_block
|
35
35
|
assert_nil @alarm_state_in_block
|
36
36
|
end
|
37
37
|
|
38
|
-
def
|
38
|
+
def test_should_initialize_dynamic_states_after_block
|
39
39
|
@machines.initialize_states(@object) do
|
40
40
|
@alarm_state_in_block = @object.alarm_state
|
41
41
|
end
|
@@ -57,10 +57,10 @@ class MachineCollectionStateInitializationTest < StateMachinesTest
|
|
57
57
|
assert_equal 'active', @object.alarm_state
|
58
58
|
end
|
59
59
|
|
60
|
-
def
|
60
|
+
def test_should_initialize_existing_static_states_by_default
|
61
61
|
@object.state = 'idling'
|
62
62
|
@machines.initialize_states(@object)
|
63
|
-
assert_equal '
|
63
|
+
assert_equal 'parked', @object.state
|
64
64
|
end
|
65
65
|
|
66
66
|
def test_should_initialize_existing_static_states_if_forced
|
@@ -69,10 +69,10 @@ class MachineCollectionStateInitializationTest < StateMachinesTest
|
|
69
69
|
assert_equal 'parked', @object.state
|
70
70
|
end
|
71
71
|
|
72
|
-
def
|
72
|
+
def test_should_initialize_existing_static_states_if_not_forced
|
73
73
|
@object.state = 'idling'
|
74
74
|
@machines.initialize_states(@object, static: true)
|
75
|
-
assert_equal '
|
75
|
+
assert_equal 'parked', @object.state
|
76
76
|
end
|
77
77
|
|
78
78
|
def test_should_skip_dynamic_states_if_disabled
|
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.1.
|
4
|
+
version: 0.1.4
|
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-01-
|
12
|
+
date: 2015-01-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -65,6 +65,7 @@ files:
|
|
65
65
|
- ".rspec"
|
66
66
|
- ".travis.yml"
|
67
67
|
- Changelog.md
|
68
|
+
- Contributors.md
|
68
69
|
- Gemfile
|
69
70
|
- LICENSE.txt
|
70
71
|
- README.md
|