simplestate 0.2.0 → 0.3.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: 450c34a364264d7b24164fde023f5bcec59b9a08
4
- data.tar.gz: cade1b14539a4590e7bcd4a36444209cabdf9d22
3
+ metadata.gz: e2ed13eeaa6d2b73c96ce7bec9e965d54ec108f0
4
+ data.tar.gz: 4e8de026210994a25eee48965ffbbb7c9c6e13ed
5
5
  SHA512:
6
- metadata.gz: eceab10c44577b6674d3307bbab54cc26d562c06cd27032937133d29ea056df1def64dd1b656a8d546027091e77e2f625936f9b4b396db33d94a745207660017
7
- data.tar.gz: c55a2fdb9a61a24471905e4605a32b9590ef0842ec312056f9105abd0c761c769a628dae4148f1021fedc4725145f09d2d5041033826d3c794628951ead2bf99
6
+ metadata.gz: 4323b583f0dd11ed6b4fa7a0dd3aae022a06d224a70aabc2ef06a40ddae2812f19ea1d30435cb6943be6d6605eba0728b357c3552c60da69e5adf16f0eb8e7fd
7
+ data.tar.gz: 2d909a5417d0b437d3a7b68882163ad5ba4d0d134612fd5f1ddcc508c7973736604137bc145fcacf3e14d472e6511a46d2a177581cbc070cfa9982986d0b148b
data/README.md CHANGED
@@ -25,7 +25,7 @@ Or install it yourself as:
25
25
  $ gem install simplestate
26
26
 
27
27
  ## Usage
28
- Inherit from StateHolder to create the class of objects that will hold states:
28
+ Inherit from StateHolder to create the class of the object that will hold states:
29
29
 
30
30
  ```ruby
31
31
  class Button < StateHolder
@@ -44,7 +44,7 @@ StateHolder expects to receive the initial state class in an opts hash at creati
44
44
  ```ruby
45
45
  button = Button.new(initial_state_class: Off)
46
46
  ```
47
- or alternate syntax:
47
+ or use this alternate syntax:
48
48
 
49
49
  ```ruby
50
50
  button = Button.new(start_in: Off)
@@ -87,6 +87,29 @@ 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:
92
+
93
+ ```ruby
94
+ class Button < StateHolder
95
+ ...
96
+
97
+ def prior_state
98
+ previous_states.last.class
99
+ end
100
+ end
101
+
102
+ # Then in tests for example:
103
+ def test_a_button_returns_its_last_prior_state
104
+ @button.press # Curr state: On, Prior state: Off
105
+ @button.press # Curr state: Off, Prior state: On
106
+ assert_equal On, @button.prior_state
107
+ end
108
+ ```
109
+
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.
111
+
112
+ # usage example
90
113
  The button module provides an example of the usage of Simplestate. Tests of this are provided in simplestate_test.rb.
91
114
 
92
115
  ## Development
data/lib/simplestate.rb CHANGED
@@ -2,3 +2,4 @@ require "delegate"
2
2
  require "simplestate/version"
3
3
  require "simplestate/state_holder"
4
4
  require "simplestate/state"
5
+ require "simplestate/nil_state"
@@ -0,0 +1,18 @@
1
+ class NilState
2
+ attr_reader :holder, :previous_state_class, :previous_states
3
+ def initialize(holder, previous_state_class)
4
+ @holder = holder
5
+ @previous_state_class = previous_state_class
6
+ @previous_states = []
7
+ end
8
+
9
+ private
10
+ def transition_to(new_state_class)
11
+ end
12
+
13
+ def enter
14
+ end
15
+
16
+ def exit
17
+ end
18
+ end
@@ -1,8 +1,9 @@
1
1
  class State
2
- attr_reader :holder, :previous_state_class
2
+ attr_reader :holder, :previous_state_class, :previous_states
3
3
  def initialize(holder, previous_state_class)
4
4
  @holder = holder
5
5
  @previous_state_class = previous_state_class
6
+ @previous_states = build_previous_states_list
6
7
  end
7
8
 
8
9
  private
@@ -17,4 +18,11 @@ private
17
18
  def exit
18
19
  raise "#{self.class.name} does not implement #exit."
19
20
  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
20
28
  end
@@ -1,8 +1,12 @@
1
1
  class StateHolder < SimpleDelegator
2
2
  def initialize(opts={})
3
- set_initial_state_from(opts)
4
- # Set current_state to initial state within SimpleDelegator
5
- super(@initial_state)
3
+ # Set current_state to nil state within SimpleDelegator
4
+ nil_state = NilState.new(nil,nil)
5
+ super(nil_state)
6
+ # Then transition to the initial state class
7
+ initial_state_class = opts.fetch :start_in,
8
+ (opts.fetch :initial_state_class, nil)
9
+ transition_to initial_state_class
6
10
  end
7
11
 
8
12
  def transition_to(new_state_class)
@@ -11,24 +15,17 @@ class StateHolder < SimpleDelegator
11
15
  current_state.send(:enter)
12
16
  end
13
17
 
18
+ def current_state
19
+ __getobj__
20
+ end
21
+
14
22
  def set_new_state(new_state_class)
15
23
  new_state = new_state_class.new(self, current_state.class)
16
24
  self.current_state = new_state
17
25
  end
18
26
 
19
- def current_state
20
- __getobj__
21
- end
22
-
23
27
  private
24
28
  def current_state=(state)
25
29
  __setobj__(state)
26
30
  end
27
-
28
- def set_initial_state_from(opts)
29
- initial_state_class = opts.fetch :start_in,
30
- (opts.fetch :initial_state_class, nil)
31
- @initial_state = initial_state_class.new(self, nil)
32
- end
33
-
34
31
  end
@@ -1,3 +1,3 @@
1
1
  module Simplestate
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/simplestate.gemspec CHANGED
@@ -30,4 +30,9 @@ Gem::Specification.new do |spec|
30
30
  spec.add_development_dependency "bundler", "~> 1.11"
31
31
  spec.add_development_dependency "rake", "~> 10.0"
32
32
  spec.add_development_dependency "minitest", "~> 5.0"
33
+
34
+ # Uncomment these to make pry available in testing
35
+ # Also make necessary changes in test_helper.rb
36
+ #spec.add_development_dependency "pry"
37
+ #spec.add_development_dependency "pry-byebug"
33
38
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simplestate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mitchell C Kuppinger
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-01-30 00:00:00.000000000 Z
11
+ date: 2016-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -71,6 +71,7 @@ files:
71
71
  - bin/console
72
72
  - bin/setup
73
73
  - lib/simplestate.rb
74
+ - lib/simplestate/nil_state.rb
74
75
  - lib/simplestate/state.rb
75
76
  - lib/simplestate/state_holder.rb
76
77
  - lib/simplestate/version.rb