simplestate 2.0.2 → 2.0.3

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: de2abc1192e14cd5e30017a8e650f25821def00a
4
- data.tar.gz: a3ee59402a6bf2780a345873fbfdd605570a7c3a
3
+ metadata.gz: 50858d8dcd747372e549f33e890087e16783a321
4
+ data.tar.gz: ac186005dd25113c4953f3e266a5cb8cab4acba9
5
5
  SHA512:
6
- metadata.gz: 9d59c31bfabcd9379ea482c780516093b2fe42d261ae96054d6683d98d5325e397d72332e4b4084591332628e38999542e8aa7dfeae5b29e32f55a704b605e0f
7
- data.tar.gz: 9fb22a378838a4240f3145356fc12d54667a0d9fa30d9f8d420cc181a021288782024c03e411cffa41a4610f4eb7e42f54e886bd8cdabe30bf6268eaaf154c58
6
+ metadata.gz: 88197c399a778dd0fedb2afeaf69d3e043ff425f86ba549ba618031d473be903c8a5c0a5f325b92cacb915c97467885ff87187e1fc8dfc142641a5d12551d920
7
+ data.tar.gz: 37d9314fd7591f9d11e5b914423da7e04a9034d6fda06b5af05b3ee2616d40d8ff25ffedf116f6b230528c5a3dc19b418b45443f9b1dfe9c2dd5b69d79070947
@@ -1,3 +1,8 @@
1
+ #### SimpleState version 2.0.3
2
+ Move maintenance of list of states available to an instance of StateList. This instance is held by the state holder. Allows multiple state holders to each have their own list of available states. Removes the effective global state being maintained in the State class.
3
+
4
+
5
+
1
6
  #### SimpleState version 2.0.0
2
7
  Ruby version requirement has changed to: __>= 2.1.0__ . (Ruby 2.0.0 can be used with some minor modification: Default values must be provided for all keyword arguments.)
3
8
 
data/README.md CHANGED
@@ -70,7 +70,7 @@ button.press #=> current_state: Off
70
70
  # Description
71
71
  Simplestate arose out of a desire for a very low ceremony mechanism to implement a state machine. SimpleDelegator (delegate.rb) is used to implement this. Because SimpleDelegator supports dynamically swapping the object to which methods are delegated, it provides a good base for Simplestate.
72
72
 
73
- The current version (2.0.0) is a rewrite of SimpleState. State logic is provided via instances of the appropriate subclasses of State. A state is referenced via a symbol created by symbolizing the state's name. The State class maintains a list of state instances available for transitions in a hash keyed on the state symbols. This is an incomplete variant of the singleton pattern. It solved some issues I was having with the previous version of SimpleState, but please use some care. For example, there is no logic to actually prevent creation of more than one instance of a given State subclass. Doing this would likely create bugs that will eventually bite you. :-(
73
+ The current version (2.0.0) is a rewrite of SimpleState. State logic is provided via instances of the appropriate subclasses of State. A state is referenced via a symbol created by symbolizing the state's name. A list of state instances available for transitions is maintained by the state holder in a hash keyed on the state symbols. This is an incomplete variant of the singleton pattern. It solved some issues I was having with the previous version of SimpleState, but please use some care. For example, there is no logic to actually prevent creation of more than one instance of a given State subclass. Doing this would likely create bugs that will eventually bite you. :-(
74
74
 
75
75
  The StateHolder class provides the required functionality for a basic state machine: methods to set the initial state and to transition to a new state. To complement this a State class is provided to serve as ancestor to the states of the state holder. A State instance stores a reference to the state holder and a private __#transition\_to__ method which simply calls the state holder’s __#transition\_to__.
76
76
 
@@ -1,7 +1,8 @@
1
1
  require "delegate"
2
2
  require "simplestate/version"
3
- require "simplestate/state_holder"
4
- require "simplestate/state_history"
5
3
  require "simplestate/state"
6
4
  require "simplestate/null_state"
5
+ require "simplestate/state_list"
6
+ require "simplestate/state_holder"
7
+ require "simplestate/state_history"
7
8
 
@@ -5,7 +5,6 @@ class NullState
5
5
  attr_reader :holder
6
6
  def initialize(holder: nil, opts: {})
7
7
  @holder = holder
8
- State.list[self.symbol]= self
9
8
  end
10
9
 
11
10
  def self.list
@@ -1,15 +1,8 @@
1
1
  class State
2
- @list = {}
3
-
4
- def self.list
5
- @list
6
- end
7
-
8
-
9
2
  attr_reader :holder
10
3
  def initialize(holder:, opts: {})
11
4
  @holder = holder
12
- State.list[self.symbol]= self
5
+ @holder.__send__(:add_state, self)
13
6
  end
14
7
 
15
8
  def name
@@ -4,6 +4,7 @@ class StateHolder < SimpleDelegator
4
4
  def initialize(initial_state:, state_history: StateHistory.new, opts: {})
5
5
  @initial_state = initial_state
6
6
  @state_history = state_history
7
+ @state_list = StateList.new
7
8
 
8
9
  super(NullState.new)
9
10
  end
@@ -32,6 +33,7 @@ class StateHolder < SimpleDelegator
32
33
  end
33
34
 
34
35
  private
36
+ attr_reader :state_list
35
37
  def leave_old_state
36
38
  current_state.__send__(:exit)
37
39
  end
@@ -43,7 +45,11 @@ class StateHolder < SimpleDelegator
43
45
  end
44
46
 
45
47
  def current_state=(state)
46
- state_obj = State.list[state]
48
+ state_obj = state_list[state]
47
49
  __setobj__(state_obj)
48
50
  end
51
+
52
+ def add_state(state_instance)
53
+ state_list.add state_instance
54
+ end
49
55
  end
@@ -0,0 +1,22 @@
1
+ class StateList
2
+ def initialize
3
+ @list = {}
4
+ end
5
+
6
+ def add(state)
7
+ @list[state.symbol] = state
8
+ end
9
+
10
+ def [](symb)
11
+ @list[symb]
12
+ end
13
+
14
+ # Convenience methods
15
+ def empty?
16
+ @list.empty?
17
+ end
18
+
19
+ def size
20
+ @list.size
21
+ end
22
+ end
@@ -1,3 +1,3 @@
1
1
  module Simplestate
2
- VERSION = "2.0.2"
2
+ VERSION = "2.0.3"
3
3
  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: 2.0.2
4
+ version: 2.0.3
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-09-06 00:00:00.000000000 Z
11
+ date: 2016-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -77,6 +77,7 @@ files:
77
77
  - lib/simplestate/state.rb
78
78
  - lib/simplestate/state_history.rb
79
79
  - lib/simplestate/state_holder.rb
80
+ - lib/simplestate/state_list.rb
80
81
  - lib/simplestate/version.rb
81
82
  - simplestate.gemspec
82
83
  homepage: https://github.com/dpneumo/simplestate