tiny_state_machine 0.1.0 → 0.1.1
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/.gitignore +1 -0
- data/README.md +6 -0
- data/lib/tiny_state_machine.rb +4 -1
- data/lib/tiny_state_machine/version.rb +1 -1
- data/spec/tiny_state_machine_spec.rb +8 -0
- metadata +1 -2
- data/lib/.DS_Store +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 047364a5b900bcbedfd987555bd54645c465c1eb
|
4
|
+
data.tar.gz: c261a785cee721df9c0a541595469316fe1ecad9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a8986e8d01ba7ae6dab57563943a6ed34c316b9297453bac7ab030af77595b9219491e0d566401388ce76aed3c70dbc1b4491d3700b6e37965b712aeae2bf95d
|
7
|
+
data.tar.gz: 1f7c801b835ef62b4705b6d03798329df084e731fb05f8f6b4a2e39c8fc56b9ffbdbb38f9aa3552b46180ea2325b144886bb7bc056ea2cd35ccb4f60b33348fa
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -25,6 +25,7 @@ sm = TinyStateMachine::Machine.new :in_store do |sm|
|
|
25
25
|
sm.event :use, :used => :used
|
26
26
|
sm.event :break, :used => :broken
|
27
27
|
sm.event :fix, :broken => :used
|
28
|
+
sm.event :earthquake, :any => :destroyed
|
28
29
|
sm.on_state_change do |event, from_state, to_state|
|
29
30
|
puts "new state = #{to_state}"
|
30
31
|
end
|
@@ -47,6 +48,11 @@ sm.state
|
|
47
48
|
sm.trigger(:use)
|
48
49
|
# new state = used
|
49
50
|
# => :used
|
51
|
+
|
52
|
+
# some events can come from :any state
|
53
|
+
sm.trigger(:earthquake)
|
54
|
+
# new state = destroyed
|
55
|
+
# => :destroyed
|
50
56
|
```
|
51
57
|
|
52
58
|
## Tests
|
data/lib/tiny_state_machine.rb
CHANGED
@@ -37,6 +37,9 @@ module TinyStateMachine
|
|
37
37
|
end
|
38
38
|
|
39
39
|
# declare events
|
40
|
+
# you can use :any in the hash key for an event that can come from any state
|
41
|
+
# @example
|
42
|
+
# sm.event :error, :any => :error
|
40
43
|
#
|
41
44
|
# @param event [String or Symbol] state name
|
42
45
|
# @param hash [Hash] a Hash in the form : old_state => new_state
|
@@ -55,7 +58,7 @@ module TinyStateMachine
|
|
55
58
|
# @raise [InvalidEvent] if the event is invalid (incorrect state)
|
56
59
|
# @return the new state the machine is in
|
57
60
|
def trigger(event)
|
58
|
-
e = @events.find { |e| e[:event] == event && e[:from] == @state }
|
61
|
+
e = @events.find { |e| e[:event] == event && (e[:from] == @state || e[:from] == :any) }
|
59
62
|
raise InvalidEvent, "Invalid event '#{event}' from state '#{@state}'" if e.nil?
|
60
63
|
old_state = @state
|
61
64
|
@state = e[:to]
|
@@ -55,6 +55,14 @@ module TinyStateMachine
|
|
55
55
|
sm.trigger(:buy)
|
56
56
|
expect(callback).to have_received(:change).with(:buy, :in_store, :new)
|
57
57
|
end
|
58
|
+
|
59
|
+
it "should handle events from :any state" do
|
60
|
+
sm = TinyStateMachine::Machine.new :in_store do |sm|
|
61
|
+
sm.event :buy, :any => :new
|
62
|
+
end
|
63
|
+
sm.trigger(:buy)
|
64
|
+
expect(sm.state).to eql :new
|
65
|
+
end
|
58
66
|
end
|
59
67
|
|
60
68
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tiny_state_machine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Geoffroy Montel
|
@@ -65,7 +65,6 @@ files:
|
|
65
65
|
- LICENSE.txt
|
66
66
|
- README.md
|
67
67
|
- Rakefile
|
68
|
-
- lib/.DS_Store
|
69
68
|
- lib/tiny_state_machine.rb
|
70
69
|
- lib/tiny_state_machine/version.rb
|
71
70
|
- spec/spec_helper.rb
|
data/lib/.DS_Store
DELETED
Binary file
|