em-scenario 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/scenario.rb +82 -42
  2. metadata +8 -8
data/lib/scenario.rb CHANGED
@@ -9,95 +9,135 @@ module EventMachine
9
9
  include EM::Deferrable
10
10
 
11
11
  def initialize &block
12
- @actions = []
13
- self.callback &block
14
- end
15
-
16
- def then &block
17
- d = EM::DefaultDeferrable.new
18
- d.callback(&block)
19
- @actions << d
12
+ @actions = AbInitioActions.new
13
+ block.call @actions
14
+ self
20
15
  end
21
16
 
22
17
  def nextStep
23
- if @actions.length > 0
24
- @actions.pop.succeed(Proc.new { nextStep })
18
+ if @actions.actions.length > 0
19
+ @actions.actions.pop.succeed(Proc.new { nextStep })
25
20
  else
26
21
  self.succeed
27
22
  end
28
23
  end
29
24
 
30
- def invoke
31
- @actions.reverse!
25
+ def finally &block
26
+ self.callback &block
27
+ @actions.actions.reverse!
32
28
  self.nextStep
33
29
  end
34
30
  end
35
31
 
32
+ class AbInitioActions
33
+ attr_accessor :actions
34
+ def initialize
35
+ @actions = []
36
+ end
37
+
38
+ def then &block
39
+ d = EM::DefaultDeferrable.new
40
+ d.callback(&block)
41
+ @actions << d
42
+ self
43
+ end
44
+
45
+ end
46
+
36
47
  #Trigger when a quota of actions is done
37
48
  class Quorum
38
49
  include EM::Deferrable
39
- def initialize
40
- @times = 0
41
- @when = false
42
- end
43
50
 
44
- def add
45
- @times += 1;
46
- yield Proc.new { finished }
51
+ def initialize times, &block
52
+ @times = times
53
+ @loop = block
54
+ self
47
55
  end
48
56
 
49
- def when(&block)
50
- @when = true
51
- self.callback(&block)
52
- finished
57
+ def finally &block
58
+ self.callback(&block)
59
+ @loop.call( Proc.new {nextStep} )
53
60
  end
54
61
 
55
62
  protected
56
- def finished
57
- @times -= 1
58
- self.succeed if @times == 0 && @when
63
+ def nextStep
64
+ if @times == 0
65
+ self.succeed
66
+ else
67
+ @times -= 1
68
+ @loop.call( Proc.new {nextStep} )
69
+ end
59
70
  end
60
71
  end
61
72
 
62
73
  # Repeat sequentially an action
63
74
  class AdLib
64
75
  include EM::Deferrable
65
- def initialize &block
66
- @cpt = 0
67
- self.callback(&block)
68
- end
69
76
 
70
- def repeat times, &block
77
+ def initialize times, &block
78
+ @cpt = 0
71
79
  @times = times
72
80
  @loop = block
73
- self._oneStep
81
+ self
74
82
  end
75
83
 
76
- def _oneStep
77
- @loop.call @cpt
78
- @cpt += 1
84
+ def finally &block
85
+ self.callback(&block)
86
+ self.nextStep
79
87
  end
80
88
 
81
- def next *arg
89
+ def nextStep
82
90
  if @cpt == @times
83
- self.succeed *arg
91
+ self.succeed
84
92
  else
85
- self._oneStep
93
+ @loop.call( Proc.new {nextStep}, @cpt)
94
+ @cpt += 1
86
95
  end
87
96
  end
97
+ end
98
+
99
+ class AdNauseum
100
+ include EM::Deferrable
101
+
102
+ def initialize &block
103
+ @loop = block
104
+ self
105
+ end
106
+
107
+ def until &block
108
+ @criteria = block
109
+ self
110
+ end
111
+
112
+ def finally &block
113
+ self.callback &block
114
+ @loop.call( Proc.new { nextStep })
115
+ self
116
+ end
88
117
 
118
+ def nextStep
119
+ if @criteria.call
120
+ self.succeed
121
+ else
122
+ @loop.call( Proc.new { nextStep })
123
+ end
124
+ end
89
125
  end
90
126
  end
91
127
  end
92
128
 
93
- def quorum
94
- EventMachine::Scenario::Quorum.new
129
+ def quorum(times, &block)
130
+ EventMachine::Scenario::Quorum.new times, &block
95
131
  end
96
132
 
97
- def adlib(&block)
98
- EventMachine::Scenario::AdLib.new &block
133
+ def adlib(times, &block)
134
+ EventMachine::Scenario::AdLib.new times, &block
99
135
  end
100
136
 
101
137
  def abinitio(&block)
102
138
  EventMachine::Scenario::AbInitio.new &block
103
139
  end
140
+
141
+ def adnauseum(&block)
142
+ EventMachine::Scenario::AdNauseum.new &block
143
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em-scenario
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-05 00:00:00.000000000 Z
12
+ date: 2011-09-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: eventmachine
16
- requirement: &2152115400 !ruby/object:Gem::Requirement
16
+ requirement: &2152000200 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - =
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.0.0.beta3
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2152115400
24
+ version_requirements: *2152000200
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: minitest
27
- requirement: &2152113020 !ruby/object:Gem::Requirement
27
+ requirement: &2151999100 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '2.0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2152113020
35
+ version_requirements: *2151999100
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &2152108000 !ruby/object:Gem::Requirement
38
+ requirement: &2151998320 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2152108000
46
+ version_requirements: *2151998320
47
47
  description: Handling simpler story with event machine's callback
48
48
  email: mathieu@garambrogne.net
49
49
  executables: []