em-scenario 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/Gemfile +3 -0
  2. data/README.md +25 -0
  3. data/lib/scenario.rb +103 -0
  4. metadata +81 -0
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'eventmachine', '~> 1.0.0.beta3'
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ Scenario
2
+ ========
3
+
4
+ Scenario provides tools for handling stories with *event machine* whithout spaghettis.
5
+
6
+ Names use kitchen's latin, because it's more leet then japanese words.
7
+
8
+ Ruby 1.9.2 is used, it may work with ruby 1.8.x
9
+
10
+ Tools
11
+ -----
12
+
13
+ The specs provide nice example of usage.
14
+
15
+ ### Quorum
16
+
17
+ Do something when n actions are done.
18
+
19
+ ### AbInitio
20
+
21
+ Act sequentialy, from the start.
22
+
23
+ ### AdLib
24
+
25
+ Repeat an action, seqentillay.
data/lib/scenario.rb ADDED
@@ -0,0 +1,103 @@
1
+ require "eventmachine"
2
+
3
+ module EventMachine
4
+ # @see http://en.wikipedia.org/wiki/List_of_Latin_phrases
5
+ module Scenario
6
+
7
+ # from the start
8
+ class AbInitio
9
+ include EM::Deferrable
10
+
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
20
+ end
21
+
22
+ def nextStep
23
+ if @actions.length > 0
24
+ @actions.pop.succeed(Proc.new { nextStep })
25
+ else
26
+ self.succeed
27
+ end
28
+ end
29
+
30
+ def invoke
31
+ @actions.reverse!
32
+ self.nextStep
33
+ end
34
+ end
35
+
36
+ #Trigger when a quota of actions is done
37
+ class Quorum
38
+ include EM::Deferrable
39
+ def initialize
40
+ @times = 0
41
+ @when = false
42
+ end
43
+
44
+ def add
45
+ @times += 1;
46
+ yield Proc.new { finished }
47
+ end
48
+
49
+ def when(&block)
50
+ @when = true
51
+ self.callback(&block)
52
+ finished
53
+ end
54
+
55
+ protected
56
+ def finished
57
+ @times -= 1
58
+ self.succeed if @times == 0 && @when
59
+ end
60
+ end
61
+
62
+ # Repeat sequentially an action
63
+ class AdLib
64
+ include EM::Deferrable
65
+ def initialize &block
66
+ @cpt = 0
67
+ self.callback(&block)
68
+ end
69
+
70
+ def repeat times, &block
71
+ @times = times
72
+ @loop = block
73
+ self._oneStep
74
+ end
75
+
76
+ def _oneStep
77
+ @loop.call @cpt
78
+ @cpt += 1
79
+ end
80
+
81
+ def next *arg
82
+ if @cpt == @times
83
+ self.succeed *arg
84
+ else
85
+ self._oneStep
86
+ end
87
+ end
88
+
89
+ end
90
+ end
91
+ end
92
+
93
+ def quorum
94
+ EventMachine::Scenario::Quorum.new
95
+ end
96
+
97
+ def adlib(&block)
98
+ EventMachine::Scenario::AdLib.new &block
99
+ end
100
+
101
+ def abinitio(&block)
102
+ EventMachine::Scenario::AbInitio.new &block
103
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: em-scenario
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mathieu Lecarme
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: eventmachine
16
+ requirement: &2152115400 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - =
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.0.beta3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2152115400
25
+ - !ruby/object:Gem::Dependency
26
+ name: minitest
27
+ requirement: &2152113020 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2152113020
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &2152108000 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2152108000
47
+ description: Handling simpler story with event machine's callback
48
+ email: mathieu@garambrogne.net
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files:
52
+ - README.md
53
+ files:
54
+ - README.md
55
+ - Gemfile
56
+ - lib/scenario.rb
57
+ homepage: http://github.com/athoune/em-scenario
58
+ licenses: []
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 1.8.10
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Handling simpler story with event machine's callback
81
+ test_files: []