lab42_state_machine 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 40cee82db2d88428e2e52f115faa98ccb571020d
4
- data.tar.gz: 8a716c44053c53ebbdc2aabfa590c8c9f0543aab
3
+ metadata.gz: 7794b97f4ada35102cd112f803d049df99e0fcdb
4
+ data.tar.gz: 5f3f4276da47d24c629f57c9f4e957c9b84e0e21
5
5
  SHA512:
6
- metadata.gz: a88d6d79918585acd61f4fed0dd0274cbe61c22cf3fd4900d145fd5844dd36231e2f3ec6cf9cddf8ebc66402e238c363050ace782ae999b11421f943bf798c58
7
- data.tar.gz: 9f7dc40f0c3a0cd3e887d9048b1574834d5cccdb85c2d81a778b9882fb823f3aa8df9a49f91cebc301a4c79046ed3c412480cd11215e32ac29af6f24feb59bde
6
+ metadata.gz: 1c5c90bb25e28f7585981f0557ead12e9a2231dc51c47e326d29a7170620b17537b36dfd4bec0fd3ab499b85d13bc222a5d940c91ccd9bc4c36627e8e2aa095f
7
+ data.tar.gz: 256aef995922fbba3c09d5380f3152fd3d343e7c7418d8566ce94a8c0bbf12edfcdabf6030ae501daee63314f68af7074a84d4da2856a58c7aa6a946ffaa6986
data/README.md CHANGED
@@ -154,7 +154,30 @@ Really part of the *Runtime API*
154
154
 
155
155
  `teardown do | last_input, last_state |` Will be called at the very end of the processing cycle.
156
156
 
157
-
158
157
  #### transition
159
158
 
160
159
  `transition a: :b, b: :c do |input, old_state, new_state|` These are execute when the state changes from :a to :b, or :b to :a. Of course only one transition can be indicated (and mostly will be). These handlers are executed *after* the state handlers fot the new states, that is :b or :c in our case.
160
+
161
+ ### Runtime API
162
+
163
+ #### halt\_machine
164
+
165
+ ```halt_machine``` stops the execution and makes run return the current `subject`
166
+
167
+ Raises a `StopIteration` and does therefore not make any sense in `setup` or `teardown` handlers
168
+
169
+
170
+ #### state
171
+
172
+ As already mentioned in the Definition and Setup API
173
+
174
+ This is the (overloaded - for the sake of a slim API) workerbee of the State Machine, it comes in three forms:
175
+
176
+ ##### state querying form (0 params)
177
+
178
+
179
+ `state` Query currrent_state of the StateMachine
180
+
181
+ ##### state setter form (1 param)
182
+
183
+ ```state new_state``` Set new state of the StateMachine
@@ -2,12 +2,14 @@ module Lab42
2
2
  class StateMachine
3
3
  class Controller
4
4
 
5
- def after &block
6
- @afters << block
5
+ def after block=nil, &blk
6
+ behavior = get_behavior block, blk
7
+ @afters << behavior
7
8
  end
8
9
 
9
- def before &block
10
- @befores << block
10
+ def before block=nil, &blk
11
+ behavior = get_behavior block, blk
12
+ @befores << behavior
11
13
  end
12
14
 
13
15
  def run input
@@ -58,7 +60,10 @@ module Lab42
58
60
  @state = st
59
61
  end
60
62
 
61
- def setup &block; @setups << block end
63
+ def setup block=nil, &blk
64
+ behavior = get_behavior block, blk
65
+ @setups << behavior
66
+ end
62
67
 
63
68
  def state *args, &block
64
69
  raise ArgumentError, "args.size = #{args.size} instead of 0..1" if args.size > 1
@@ -78,7 +83,10 @@ module Lab42
78
83
  private
79
84
  def current_transition; [@old_state, @state] end
80
85
 
81
- def mk_ary_hash; Hash.new{ |h,k| h[k] = [] } end
86
+ def get_behavior block1, block2
87
+ raise ArgumentError, "need exactly one block ( or symbol )" unless !!block1 != !!block2
88
+ mk_block block1 || block2
89
+ end
82
90
 
83
91
  def initialize state_machine
84
92
  @sm = state_machine
@@ -93,6 +101,19 @@ module Lab42
93
101
  @handlers = mk_ary_hash
94
102
  @transitions = mk_ary_hash
95
103
  end
104
+
105
+ def mk_block blocky
106
+ case blocky
107
+ when Proc, Method
108
+ blocky
109
+ when Symbol, String
110
+ @sm.method blocky
111
+ else
112
+ blocky.to_proc
113
+ end
114
+ end
115
+
116
+ def mk_ary_hash; Hash.new{ |h,k| h[k] = [] } end
96
117
  end # class Controller
97
118
  end # class StateMachine
98
119
  end # module Lab42
@@ -1,5 +1,5 @@
1
1
  module Lab42
2
2
  class StateMachine
3
- Version = "0.1.1"
3
+ Version = "0.1.2"
4
4
  end # class StateMachine
5
5
  end # module Lab42
@@ -8,6 +8,12 @@ module Lab42
8
8
 
9
9
  forward_all :after, :before, :setup, :state, :teardown, :transition, to: :controller
10
10
 
11
+ def behavior name, &behave
12
+ class << self; self end. module_eval do
13
+ define_method name do behave end
14
+ end
15
+ end
16
+
11
17
  def halt_machine
12
18
  raise StopIteration, "#{self} halted"
13
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lab42_state_machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
@@ -83,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
83
  version: '0'
84
84
  requirements: []
85
85
  rubyforge_project:
86
- rubygems_version: 2.0.3
86
+ rubygems_version: 2.1.0
87
87
  signing_key:
88
88
  specification_version: 4
89
89
  summary: A Simple State Machine