simplestate 0.1.6 → 0.2.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 +18 -7
- data/lib/simplestate/state_holder.rb +10 -4
- 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: 450c34a364264d7b24164fde023f5bcec59b9a08
|
|
4
|
+
data.tar.gz: cade1b14539a4590e7bcd4a36444209cabdf9d22
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: eceab10c44577b6674d3307bbab54cc26d562c06cd27032937133d29ea056df1def64dd1b656a8d546027091e77e2f625936f9b4b396db33d94a745207660017
|
|
7
|
+
data.tar.gz: c55a2fdb9a61a24471905e4605a32b9590ef0842ec312056f9105abd0c761c769a628dae4148f1021fedc4725145f09d2d5041033826d3c794628951ead2bf99
|
data/README.md
CHANGED
|
@@ -38,7 +38,23 @@ class Button < StateHolder
|
|
|
38
38
|
# button methods here
|
|
39
39
|
end
|
|
40
40
|
```
|
|
41
|
-
|
|
41
|
+
|
|
42
|
+
StateHolder expects to receive the initial state class in an opts hash at creation. Creation of a holder instance *must* specify the initial state class:
|
|
43
|
+
|
|
44
|
+
```ruby
|
|
45
|
+
button = Button.new(initial_state_class: Off)
|
|
46
|
+
```
|
|
47
|
+
or alternate syntax:
|
|
48
|
+
|
|
49
|
+
```ruby
|
|
50
|
+
button = Button.new(start_in: Off)
|
|
51
|
+
```
|
|
52
|
+
If you want to set additional attributes at creation of a new button, do so within the opts hash when new is called. Set the attribute from the opts hash in initialize. You *must* call super if you provide an initialize method in your holder class.
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
button = Button.new(start_in: Off, color: 'Red')
|
|
56
|
+
```
|
|
57
|
+
|
|
42
58
|
|
|
43
59
|
Inherit from State to create a class to provide specific state behaviors:
|
|
44
60
|
|
|
@@ -63,18 +79,13 @@ private
|
|
|
63
79
|
end
|
|
64
80
|
end
|
|
65
81
|
```
|
|
66
|
-
The subclassed state may provide *private* enter and exit methods. Any other state methods intended to be available via a call
|
|
82
|
+
The subclassed state may provide *private* enter and exit methods. Any other state methods intended to be available via a method call on the state holder must be public. #enter and #exit will always be called appropriately during state transitions.
|
|
67
83
|
|
|
68
84
|
A state has access to methods on the state holder via #holder:
|
|
69
85
|
|
|
70
86
|
```ruby
|
|
71
87
|
holder.a_special_holder_method
|
|
72
88
|
```
|
|
73
|
-
Creation of a holder instance *must* specify the initial state class:
|
|
74
|
-
|
|
75
|
-
```ruby
|
|
76
|
-
button = Button.new(initial_state_class: Off)
|
|
77
|
-
```
|
|
78
89
|
|
|
79
90
|
The button module provides an example of the usage of Simplestate. Tests of this are provided in simplestate_test.rb.
|
|
80
91
|
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
class StateHolder < SimpleDelegator
|
|
2
2
|
def initialize(opts={})
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
super(initial_state)
|
|
3
|
+
set_initial_state_from(opts)
|
|
4
|
+
# Set current_state to initial state within SimpleDelegator
|
|
5
|
+
super(@initial_state)
|
|
7
6
|
end
|
|
8
7
|
|
|
9
8
|
def transition_to(new_state_class)
|
|
@@ -25,4 +24,11 @@ private
|
|
|
25
24
|
def current_state=(state)
|
|
26
25
|
__setobj__(state)
|
|
27
26
|
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
|
+
|
|
28
34
|
end
|
data/lib/simplestate/version.rb
CHANGED