state_jacket 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +58 -1
- data/lib/state_jacket/catalog.rb +13 -0
- data/lib/state_jacket/version.rb +1 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -11,7 +11,7 @@ and how they transition to other states.
|
|
11
11
|
|
12
12
|
*The examples below are somewhat contrived, but should clearly illustrate the usage.*
|
13
13
|
|
14
|
-
##
|
14
|
+
## The Basics
|
15
15
|
|
16
16
|
#### Install
|
17
17
|
|
@@ -32,6 +32,7 @@ states.add :closed => [:open, :error]
|
|
32
32
|
states.add :error
|
33
33
|
states.lock
|
34
34
|
|
35
|
+
states.inspect # => {:open=>[:closed, :error], :closed=>[:open, :error], :error=>nil}
|
35
36
|
states.transitioners # => [:open, :closed]
|
36
37
|
states.terminators # => [:error]
|
37
38
|
|
@@ -71,3 +72,59 @@ states.can_transition? :connected => :idle # => true
|
|
71
72
|
states.can_transition? :idle => [:dialing, :connected] # => false
|
72
73
|
```
|
73
74
|
|
75
|
+
## Deep Cuts
|
76
|
+
|
77
|
+
Lets add state awareness and behavior to another class.
|
78
|
+
We'll reuse the turnstyle states from the example from above.
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
require "state_jacket"
|
82
|
+
|
83
|
+
class Turnstyle
|
84
|
+
attr_reader :states, :current_state
|
85
|
+
|
86
|
+
def initialize
|
87
|
+
@states = StateJacket::Catalog.new
|
88
|
+
@states.add :open => [:closed, :error]
|
89
|
+
@states.add :closed => [:open, :error]
|
90
|
+
@states.add :error
|
91
|
+
@states.lock
|
92
|
+
@current_state = :closed
|
93
|
+
end
|
94
|
+
|
95
|
+
def open
|
96
|
+
if states.can_transition? current_state => :open
|
97
|
+
@current_state = :open
|
98
|
+
else
|
99
|
+
raise "Can't transition from #{@current_state} to :open"
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def close
|
104
|
+
if states.can_transition? current_state => :closed
|
105
|
+
@current_state = :closed
|
106
|
+
else
|
107
|
+
raise "Can't transition from #{@current_state} to :closed"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def break
|
112
|
+
@current_state = :error
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
# example usage
|
117
|
+
turnstyle = Turnstyle.new
|
118
|
+
turnstyle.current_state # => :closed
|
119
|
+
turnstyle.open
|
120
|
+
turnstyle.current_state # => :open
|
121
|
+
turnstyle.close
|
122
|
+
turnstyle.current_state # => :closed
|
123
|
+
turnstyle.close # => RuntimeError: Can't transition from closed to :closed
|
124
|
+
turnstyle.open
|
125
|
+
turnstyle.current_state # => :open
|
126
|
+
turnstyle.open # => RuntimeError: Can't transition from open to :open
|
127
|
+
turnstyle.break
|
128
|
+
turnstyle.open # => RuntimeError: Can't transition from error to :open
|
129
|
+
turnstyle.close # => RuntimeError: Can't transition from error to :closed
|
130
|
+
```
|
data/lib/state_jacket/catalog.rb
CHANGED
@@ -31,12 +31,20 @@ module StateJacket
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
+
def transitioner?(state)
|
35
|
+
transitioners.include?(state.to_sym)
|
36
|
+
end
|
37
|
+
|
34
38
|
def terminators
|
35
39
|
keys.select do |state|
|
36
40
|
self[state] == nil
|
37
41
|
end
|
38
42
|
end
|
39
43
|
|
44
|
+
def terminator?(state)
|
45
|
+
terminators.include?(state.to_sym)
|
46
|
+
end
|
47
|
+
|
40
48
|
def lock
|
41
49
|
values.flatten.each do |value|
|
42
50
|
next if value.nil?
|
@@ -46,6 +54,11 @@ module StateJacket
|
|
46
54
|
end
|
47
55
|
freeze
|
48
56
|
end
|
57
|
+
|
58
|
+
def supports_state?(state)
|
59
|
+
keys.include?(state.to_sym)
|
60
|
+
end
|
61
|
+
|
49
62
|
end
|
50
63
|
|
51
64
|
end
|
data/lib/state_jacket/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: state_jacket
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-22 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description:
|
15
15
|
email:
|
@@ -50,7 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
50
50
|
version: '0'
|
51
51
|
requirements: []
|
52
52
|
rubyforge_project:
|
53
|
-
rubygems_version: 1.8.
|
53
|
+
rubygems_version: 1.8.24
|
54
54
|
signing_key:
|
55
55
|
specification_version: 3
|
56
56
|
summary: Intuitively define state machine like states and transitions.
|