simplestate 0.3.1 → 1.0.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/README.md +13 -7
- data/lib/simplestate/nil_state.rb +2 -4
- data/lib/simplestate/state.rb +2 -11
- data/lib/simplestate/state_holder.rb +12 -2
- data/lib/simplestate/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3c899bf018689c01a33b5d830d07899ce62ec930
|
|
4
|
+
data.tar.gz: f36c330f1c1e152f24573ceecbf1cdb55047c231
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0e9664a43777c82389fe56d9af35f3c3246d5e3b92de9850e3f7c188140c2ffae6b96c27594897cf02860d73feef0908c5b65c358d3f691592e6134df6e6f9a0
|
|
7
|
+
data.tar.gz: d3a4ff105ccf61640de63c467b3f093e245695eda1b825719289a96263603c0c3a0ba4c429b696ad48f4a21676132b40d96abbd6adaaa3bd1790fad6a4ded15e
|
data/README.md
CHANGED
|
@@ -87,19 +87,22 @@ A state has access to methods on the state holder via #holder:
|
|
|
87
87
|
holder.a_special_holder_method
|
|
88
88
|
```
|
|
89
89
|
|
|
90
|
-
|
|
91
|
-
A state
|
|
90
|
+
#### version 1.0.0
|
|
91
|
+
A state holder tracks the history of state transitions in an array accessed via #state_history. The array size defaults to 5. The last item in the array will be the most recent previous state instance. The size may be set at holder creation in the opts hash (:hx_size_limit). The history size limit has a getter and a setter defined as well. (#hx_size_limit= & #hx_size_limit).
|
|
92
92
|
|
|
93
93
|
```ruby
|
|
94
94
|
class Button < StateHolder
|
|
95
95
|
...
|
|
96
|
-
|
|
97
96
|
def prior_state
|
|
98
|
-
|
|
97
|
+
state_history.last.class
|
|
99
98
|
end
|
|
100
99
|
end
|
|
101
100
|
|
|
102
101
|
# Then in tests for example:
|
|
102
|
+
def setup
|
|
103
|
+
@button = Button.new(start_in: Off, color: 'Red', hz_size_limit: 3)
|
|
104
|
+
end
|
|
105
|
+
|
|
103
106
|
def test_a_button_returns_its_last_prior_state
|
|
104
107
|
@button.press # Curr state: On, Prior state: Off
|
|
105
108
|
@button.press # Curr state: Off, Prior state: On
|
|
@@ -107,10 +110,13 @@ def test_a_button_returns_its_last_prior_state
|
|
|
107
110
|
end
|
|
108
111
|
```
|
|
109
112
|
|
|
110
|
-
Please note that State
|
|
113
|
+
Please note that the State instance method, previous_state_class, has been removed in this release.
|
|
114
|
+
|
|
115
|
+
#### version 0.3.0 addition
|
|
116
|
+
The 0.3.0 version contained a serious code smell: A state was expected to know about the history of state transitions. However, a state should know only the states to which it may transition and it's holder to support triggering those transitions. Knowlege of the transition history belongs with the state holder, if it is tracked at all.
|
|
111
117
|
|
|
112
|
-
|
|
113
|
-
The button module provides an example of the usage of Simplestate. Tests of this are provided in simplestate_test.rb.
|
|
118
|
+
#### usage example
|
|
119
|
+
The button module (test/dummys/button.rb) provides an example of the usage of Simplestate. Tests of this are provided in simplestate_test.rb.
|
|
114
120
|
|
|
115
121
|
## Development
|
|
116
122
|
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
class NilState
|
|
2
|
-
attr_reader :holder
|
|
3
|
-
def initialize(holder
|
|
2
|
+
attr_reader :holder
|
|
3
|
+
def initialize(holder)
|
|
4
4
|
@holder = holder
|
|
5
|
-
@previous_state_class = previous_state_class
|
|
6
|
-
@previous_states = []
|
|
7
5
|
end
|
|
8
6
|
|
|
9
7
|
private
|
data/lib/simplestate/state.rb
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
class State
|
|
2
|
-
attr_reader :holder
|
|
3
|
-
def initialize(holder
|
|
2
|
+
attr_reader :holder
|
|
3
|
+
def initialize(holder)
|
|
4
4
|
@holder = holder
|
|
5
|
-
@previous_state_class = previous_state_class
|
|
6
|
-
@previous_states = build_previous_states_list
|
|
7
5
|
end
|
|
8
6
|
|
|
9
7
|
private
|
|
@@ -18,11 +16,4 @@ private
|
|
|
18
16
|
def exit
|
|
19
17
|
raise "#{self.class.name} does not implement #exit."
|
|
20
18
|
end
|
|
21
|
-
|
|
22
|
-
def build_previous_states_list
|
|
23
|
-
hcs = holder.current_state
|
|
24
|
-
psa = hcs.previous_states || []
|
|
25
|
-
psa.shift
|
|
26
|
-
psa << hcs
|
|
27
|
-
end
|
|
28
19
|
end
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
class StateHolder < SimpleDelegator
|
|
2
|
+
attr_reader :state_history
|
|
3
|
+
attr_accessor :hx_size_limit
|
|
2
4
|
def initialize(opts={})
|
|
5
|
+
@state_history = []
|
|
6
|
+
@hx_size_limit = opts.fetch :hx_size_limit, 5
|
|
3
7
|
# Set current_state to nil state within SimpleDelegator
|
|
4
|
-
nil_state = NilState.new(nil
|
|
8
|
+
nil_state = NilState.new(nil)
|
|
5
9
|
super(nil_state)
|
|
6
10
|
# Then transition to the initial state class
|
|
7
11
|
initial_state_class = opts.fetch :start_in,
|
|
@@ -10,6 +14,7 @@ class StateHolder < SimpleDelegator
|
|
|
10
14
|
end
|
|
11
15
|
|
|
12
16
|
def transition_to(new_state_class)
|
|
17
|
+
update_state_history
|
|
13
18
|
current_state.send(:exit)
|
|
14
19
|
set_new_state(new_state_class)
|
|
15
20
|
current_state.send(:enter)
|
|
@@ -20,7 +25,7 @@ class StateHolder < SimpleDelegator
|
|
|
20
25
|
end
|
|
21
26
|
|
|
22
27
|
def set_new_state(new_state_class)
|
|
23
|
-
new_state = new_state_class.new(self
|
|
28
|
+
new_state = new_state_class.new(self)
|
|
24
29
|
self.current_state = new_state
|
|
25
30
|
end
|
|
26
31
|
|
|
@@ -28,4 +33,9 @@ private
|
|
|
28
33
|
def current_state=(state)
|
|
29
34
|
__setobj__(state)
|
|
30
35
|
end
|
|
36
|
+
|
|
37
|
+
def update_state_history
|
|
38
|
+
@state_history << current_state
|
|
39
|
+
@state_history = @state_history.last(hx_size_limit)
|
|
40
|
+
end
|
|
31
41
|
end
|
data/lib/simplestate/version.rb
CHANGED