stateology 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README +40 -0
- data/lib/stateology.rb +5 -2
- data/sample.rb +45 -0
- metadata +1 -1
data/README
CHANGED
@@ -4,6 +4,7 @@ Supports:
|
|
4
4
|
* Dynamic switching between states (mixing and unmixing modules)
|
5
5
|
* Clean DSL-style syntax
|
6
6
|
* Optional state_entry() and state_exit() hooks for each state (automatically called upon state entry and exit)
|
7
|
+
* support for subclassing of classes that include Stateology (see below)
|
7
8
|
|
8
9
|
Use as in the following:
|
9
10
|
|
@@ -77,6 +78,42 @@ s.do_something #=> "Kicks a puppy"
|
|
77
78
|
s.state nil
|
78
79
|
s.do_something #=> "stares at the ceiling"
|
79
80
|
|
81
|
+
UPDATE:
|
82
|
+
* made it so subclasses can inherit states from their superclasses e.g
|
83
|
+
class A
|
84
|
+
include Stateology
|
85
|
+
|
86
|
+
state(:Happy) {
|
87
|
+
def state_entry
|
88
|
+
puts "entering Happy state"
|
89
|
+
end
|
90
|
+
|
91
|
+
def hello
|
92
|
+
puts "hello from A"
|
93
|
+
end
|
94
|
+
}
|
95
|
+
end
|
96
|
+
|
97
|
+
class B < A
|
98
|
+
state(:Happy) {
|
99
|
+
def hello
|
100
|
+
puts "hello from B"
|
101
|
+
end
|
102
|
+
}
|
103
|
+
end
|
104
|
+
|
105
|
+
b = B.new
|
106
|
+
|
107
|
+
b.state :Happy
|
108
|
+
#=> "entering Happy state"
|
109
|
+
|
110
|
+
b.hello
|
111
|
+
#=> "hello from B"
|
112
|
+
|
113
|
+
* prior behaviour was for state_entry not to exist in class B as Happy module from class A was overwritten by the new Happy module in B
|
114
|
+
* how does this fix work? the Happy module in B just includes any extant Happy module accessible in B
|
115
|
+
|
116
|
+
|
80
117
|
|
81
118
|
|
82
119
|
---=A FEW THINGS TO NOTE=---
|
@@ -99,6 +136,9 @@ In the above the string "hello" is passed as a parameter to the state_entry() me
|
|
99
136
|
* The #state method can accept either a Symbol (e.g :Happy) or a Module (e.g Happy or Sample::Happy). The following are equivalent:
|
100
137
|
s.state :Happy #=> change state to Happy
|
101
138
|
|
139
|
+
* The #state method can take a block; the block will be executed after the successful change of state:
|
140
|
+
e.g s.state(:Happy) { s.hello } #=> hello method invoked immediately after change of state as it's in the block
|
141
|
+
|
102
142
|
s.state Sample::Happy #=> equivalent to above (note the fully qualified name; as Happy is a module defined under the Sample class)
|
103
143
|
|
104
144
|
* alternatively; if the #state method is invoked internally by another instance method of the Sample class then a fully qualified module name is not required:
|
data/lib/stateology.rb
CHANGED
@@ -65,7 +65,7 @@ module Stateology
|
|
65
65
|
|
66
66
|
end
|
67
67
|
|
68
|
-
def state(*state_args)
|
68
|
+
def state(*state_args, &block)
|
69
69
|
|
70
70
|
# behave as getter
|
71
71
|
if(state_args.empty?) then
|
@@ -89,10 +89,13 @@ module Stateology
|
|
89
89
|
|
90
90
|
# enter new state
|
91
91
|
__state_prologue(state_name, state_args)
|
92
|
-
|
92
|
+
|
93
93
|
# update the current state variable
|
94
94
|
@__SM_cur_state = state_name
|
95
95
|
|
96
|
+
# if we were given a block, run it now
|
97
|
+
if(block) then yield end
|
98
|
+
|
96
99
|
rescue NameError
|
97
100
|
raise NameError, "#{state_name} not a valid state"
|
98
101
|
|
data/sample.rb
CHANGED
@@ -53,7 +53,18 @@ class Sample
|
|
53
53
|
|
54
54
|
end
|
55
55
|
|
56
|
+
class SampleChild < Sample
|
57
|
+
state(:Happy) {
|
58
|
+
def do_something
|
59
|
+
puts "pets a Kitten"
|
60
|
+
end
|
61
|
+
}
|
62
|
+
end
|
63
|
+
|
56
64
|
s = Sample.new
|
65
|
+
sc = SampleChild.new
|
66
|
+
|
67
|
+
puts "Testing Sample"
|
57
68
|
|
58
69
|
# in Default state
|
59
70
|
s.do_something #=> "stares at the ceiling"
|
@@ -75,6 +86,40 @@ s.state :Angry
|
|
75
86
|
# what state are we in?
|
76
87
|
puts s.state
|
77
88
|
|
89
|
+
# pass a block to state transition
|
90
|
+
puts "passing a block"
|
91
|
+
s.state :Happy do
|
92
|
+
s.do_something; s.do_something; s.do_something
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
puts "**********************"
|
97
|
+
puts "Now Testing SampleChild"
|
98
|
+
# in Default state
|
99
|
+
sc.do_something #=> "stares at the ceiling"
|
100
|
+
|
101
|
+
# now switch to Happy state
|
102
|
+
sc.state :Happy
|
103
|
+
sc.do_something #=> "Pets a puppy"
|
104
|
+
|
105
|
+
# now switch to Angry state
|
106
|
+
sc.state Sample::Angry
|
107
|
+
sc.do_something #=> "Kicks a puppy"
|
108
|
+
|
109
|
+
# now switch back to Default state
|
110
|
+
sc.state nil
|
111
|
+
sc.do_something #=> "stares at the ceiling"
|
112
|
+
|
113
|
+
sc.state :Angry
|
114
|
+
|
115
|
+
# what state are we in?
|
116
|
+
puts sc.state
|
117
|
+
|
118
|
+
# pass a block to state transition
|
119
|
+
puts "passing a block"
|
120
|
+
sc.state :Happy do
|
121
|
+
sc.do_something; sc.do_something; sc.do_something
|
122
|
+
end
|
78
123
|
|
79
124
|
|
80
125
|
|