lab42_state_machine 0.2.0 → 0.2.1

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: 1a04c2bcbca5f354f981c2f929f862ca3dde8d20
4
- data.tar.gz: 28f8f7ab95247ebbe85b8f0738e7971642e42d20
3
+ metadata.gz: 9cb027906651d393d34115d3caeb20cbbc1d21fe
4
+ data.tar.gz: 7aaf1ebbe0a829426fb37af2ae98c0b90f20e540
5
5
  SHA512:
6
- metadata.gz: 4c2391b75893b6779206664fc468b8ce289fcb42b59b27f1613a30e9a5a02819d6d433111b3299d0a4c82e8ab198cef718915488d5394838fd1673c06d50b240
7
- data.tar.gz: ad0f241232884a58993dccf3997b9629cea8d7ae25e85b45a4cabb70c478144a21d4213e643d8b35a675cb68c34c3a05af10f2c191c5520761fa1e2ab04def4c
6
+ metadata.gz: ba307dc46b61393a908927b31672083af04411b425cd55d3347bf68c2db987c8a6d76d0672f2e4aa7be80c8bd85c70770fec7c5910dfa39f1970d92edb201887
7
+ data.tar.gz: 4e5990cf6a562b00ed0aed7c1176f1906750972461c4e951cb4ea91f0dcf9739d9e55f9ea42ac00c6309c7161a2103828643abe49a4c1669d36f54dc71f64c7f
@@ -14,6 +14,15 @@ module Lab42
14
14
  end
15
15
  end
16
16
 
17
+ def clone new_subject=nil, &redefinition
18
+ old_controller = controller
19
+ copy = self.class.new( new_subject || subject.clone ){
20
+ controller.send :inherit_from!, old_controller
21
+ }
22
+ copy.instance_eval( &redefinition ) if redefinition
23
+ copy
24
+ end
25
+
17
26
  def halt_machine
18
27
  raise StopIteration, "#{self} halted"
19
28
  end
@@ -35,7 +35,9 @@ module Lab42
35
35
  end
36
36
 
37
37
  def run_one
38
- @current_input = stream.next
38
+ @current_input = stream.peek
39
+ @stream = stream.drop 1
40
+
39
41
  run_befores
40
42
  run_states
41
43
  run_transitions
@@ -109,6 +111,15 @@ module Lab42
109
111
 
110
112
  private
111
113
  def current_transition; [@old_state, @state] end
114
+
115
+ def inherit_from! old_controller
116
+ raise ArgumentError, "not a controller #{old_controller}" unless self.class === old_controller
117
+
118
+ %w{setups teardowns afters befores handlers transitions}.each do | handlers |
119
+ instance_variable_set( "@#{handlers}",
120
+ old_controller.instance_variable_get( "@#{handlers}" ).clone )
121
+ end
122
+ end
112
123
 
113
124
  def initialize state_machine
114
125
  @sm = state_machine
@@ -140,7 +151,7 @@ module Lab42
140
151
  def mk_input_stream input
141
152
  case input
142
153
  when Enumerable, Enumerator
143
- @stream = input.lazy
154
+ @original_stream = @stream = input.lazy
144
155
  else
145
156
  raise ArgumentError, "cannot enumerate on object #{input}"
146
157
  end
@@ -5,7 +5,7 @@ module Lab42
5
5
  module StreamApi
6
6
 
7
7
  extend Forwarder
8
- forward :rewind, to: :stream
8
+ forward_all :count, :take,:take_while, to: :stream
9
9
 
10
10
  class ::Proc
11
11
  def negated
@@ -29,10 +29,41 @@ module Lab42
29
29
  @sm.skip
30
30
  end
31
31
 
32
+ def has_next?
33
+ stream.peek
34
+ true
35
+ rescue StopIteration
36
+ false
37
+ end
38
+ def last?; !has_next? end
39
+
40
+ def peek *args
41
+ stream.peek
42
+ rescue StopIteration
43
+ args.first
44
+ end
45
+
46
+ def reject &filter
47
+ @stream = @stream.reject( &filter )
48
+ end
49
+
50
+ def rewind
51
+ @stream = @original_stream
52
+ end
53
+
32
54
  def rewind!
33
55
  rewind
34
56
  @sm.skip
35
57
  end
58
+
59
+ def select &filter
60
+ @stream = @stream.select( &filter )
61
+ end
62
+
63
+ def take_until &condition
64
+ take_while( &condition.negated )
65
+ end
66
+
36
67
  end # module StreamApi
37
68
  end # class Controller
38
69
  end # class StateMachine
@@ -1,5 +1,5 @@
1
1
  module Lab42
2
2
  class StateMachine
3
- Version = "0.2.0"
3
+ Version = "0.2.1"
4
4
  end # class StateMachine
5
5
  end # module Lab42
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lab42_state_machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-11 00:00:00.000000000 Z
11
+ date: 2013-09-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: forwarder19