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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9fdb020f0abef3fdccbc30bf720dbc35047ffd14
4
- data.tar.gz: 0304e8b3a88cbf05244332fe05b291a5f411b0f4
3
+ metadata.gz: 3c899bf018689c01a33b5d830d07899ce62ec930
4
+ data.tar.gz: f36c330f1c1e152f24573ceecbf1cdb55047c231
5
5
  SHA512:
6
- metadata.gz: 29d884b9ab06f2a216695e9e8750f241954423f005a2a1cad82fdec60c783484905c49c99aec30f2418c35a513303166479cbe59bec862c801970512ca45783f
7
- data.tar.gz: a2a05dcb6a56e3309137a8ccd93124d08cf52135baba0760eb0a491de3475e2052e66208f124ae91a7744d496704c25eb691eea31e98b18669f90bad06a43cd0
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
- ### version 0.3.0 addition
91
- A state has a *previous_states* method that returns an array of the states through which the state holder has transitioned to reach that state. Currently that array size is limited to 1. Old entries are dropped as new entries are added. DO NOT rely on that size limit. The last item in the list will be the most recent previous state. This is a public method on a state so it can be used via the state holder:
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
- previous_states.last.class
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#previous_state_class does not have to agree with previous_states.last.class. #previous_state_class is set arbitrarily at instance creation. #previous_states is updated automatically at each state transition. #previous_state_class will be removed at some point in the future.
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
- ### usage example
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, :previous_state_class, :previous_states
3
- def initialize(holder, previous_state_class)
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
@@ -1,9 +1,7 @@
1
1
  class State
2
- attr_reader :holder, :previous_state_class, :previous_states
3
- def initialize(holder, previous_state_class)
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,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, current_state.class)
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
@@ -1,3 +1,3 @@
1
1
  module Simplestate
2
- VERSION = "0.3.1"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simplestate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mitchell C Kuppinger