callback-batch 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+
5
+ # Add dependencies to develop your gem here.
6
+ # Include everything needed to run rake, tests, features, etc.
7
+ group :development do
8
+ gem "bundler", ">= 1.0.0"
9
+ gem "jeweler", ">= 1.5.2"
10
+ gem "riot", ">= 0.12.3"
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.6.4)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.9.2)
10
+ riot (0.12.5)
11
+ rr
12
+ rr (1.0.4)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ bundler (>= 1.0.0)
19
+ jeweler (>= 1.5.2)
20
+ riot (>= 0.12.3)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ Callback Batch
2
+ ==============
3
+
4
+ **callback-batch** provides ability to run more callbacked methods
5
+ of single or more objects by elegant, readable and transparent way
6
+ in a linear sequence, so subsequently in single batch.
7
+
8
+ Two classes are available, *sequencer* and more general *batch*.
9
+ Batch supports more objects in one batch, sequencer is syntactic
10
+ sugar in fact for single object.
11
+
12
+ See some trivial examples:
13
+
14
+ require "callback-batch"
15
+
16
+ class Foo
17
+ def foo1
18
+ yield :foo1
19
+ end
20
+ def foo2
21
+ yield :foo2
22
+ end
23
+ end
24
+
25
+ class Bar
26
+ def bar1
27
+ yield :bar1
28
+ end
29
+ def bar2
30
+ yield :bar2
31
+ end
32
+ end
33
+
34
+
35
+ ### Sequencer
36
+
37
+ s = CallbackSequencer::new(Foo::new)
38
+ s.foo1
39
+ s.foo2
40
+
41
+ s.execute do # now will be both methods executed
42
+ p s.results # will contain [:foo1, :foo2]
43
+ end
44
+
45
+ ### Batch
46
+
47
+ s = CallbackBatch::new
48
+ f = Foo::new
49
+ b = Bar::new
50
+
51
+ s.take(f).foo1
52
+ s.take(b).bar2
53
+
54
+ s.execute do # now will be both methods executed
55
+ p s.results # will contain [:foo1, :bar2]
56
+ end
57
+
58
+
59
+ Contributing
60
+ ------------
61
+
62
+ 1. Fork it.
63
+ 2. Create a branch (`git checkout -b 20101220-my-change`).
64
+ 3. Commit your changes (`git commit -am "Added something"`).
65
+ 4. Push to the branch (`git push origin 20101220-my-change`).
66
+ 5. Create an [Issue][9] with a link to your branch.
67
+ 6. Enjoy a refreshing Diet Coke and wait.
68
+
69
+
70
+ Copyright
71
+ ---------
72
+
73
+ Copyright © 2011 [Martin Kozák][10]. See `LICENSE.txt` for
74
+ further details.
75
+
76
+ [9]: http://github.com/martinkozak/callback-batch/issues
77
+ [10]: http://www.martinkozak.net/
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'bundler'
4
+
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+
13
+ require 'rake'
14
+ require 'jeweler'
15
+
16
+ Jeweler::Tasks.new do |gem|
17
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
18
+ gem.name = "callback-batch"
19
+ gem.homepage = "http://github.com/martinkozak/callback-batch"
20
+ gem.license = "MIT"
21
+ gem.summary = "Provides ability to run more callbacked methods of single or more objects by elegant, readable and transparent way in a linear sequence, so subsequently in single batch."
22
+ gem.email = "martinkozak@martinkozak.net"
23
+ gem.authors = ["Martin Kozák"]
24
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
25
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
26
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
27
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
28
+ end
29
+ Jeweler::RubygemsDotOrgTasks.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,54 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "callback-batch"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Martin Koz\u{e1}k"]
12
+ s.date = "2011-10-17"
13
+ s.email = "martinkozak@martinkozak.net"
14
+ s.extra_rdoc_files = [
15
+ "LICENSE.txt",
16
+ "README.md"
17
+ ]
18
+ s.files = [
19
+ ".document",
20
+ "Gemfile",
21
+ "Gemfile.lock",
22
+ "LICENSE.txt",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "callback-batch.gemspec",
27
+ "lib/callback-batch.rb",
28
+ "test"
29
+ ]
30
+ s.homepage = "http://github.com/martinkozak/callback-batch"
31
+ s.licenses = ["MIT"]
32
+ s.require_paths = ["lib"]
33
+ s.rubygems_version = "1.8.11"
34
+ s.summary = "Provides ability to run more callbacked methods of single or more objects by elegant, readable and transparent way in a linear sequence, so subsequently in single batch."
35
+
36
+ if s.respond_to? :specification_version then
37
+ s.specification_version = 3
38
+
39
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
40
+ s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
41
+ s.add_development_dependency(%q<jeweler>, [">= 1.5.2"])
42
+ s.add_development_dependency(%q<riot>, [">= 0.12.3"])
43
+ else
44
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
45
+ s.add_dependency(%q<jeweler>, [">= 1.5.2"])
46
+ s.add_dependency(%q<riot>, [">= 0.12.3"])
47
+ end
48
+ else
49
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
50
+ s.add_dependency(%q<jeweler>, [">= 1.5.2"])
51
+ s.add_dependency(%q<riot>, [">= 0.12.3"])
52
+ end
53
+ end
54
+
@@ -0,0 +1,242 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ ##
5
+ # Sequencer for running batches upon single object.
6
+ #
7
+
8
+ class CallbackSequencer
9
+
10
+ ##
11
+ # Holds the batch object.
12
+ # @return [EM::Batch]
13
+ #
14
+
15
+ attr_accessor :batch
16
+ @batch
17
+
18
+ ##
19
+ # Holds the target object.
20
+ # @return [Object]
21
+ #
22
+
23
+ attr_accessor :target
24
+ @target
25
+
26
+ ##
27
+ # Constructor.
28
+ # @param [Object] target target object
29
+ #
30
+
31
+ def initialize(target)
32
+ @batch = CallbackBatch::new
33
+ @target = target
34
+ end
35
+
36
+ ##
37
+ # Handles missing method calls. Puts them to the
38
+ # processing batch.
39
+ #
40
+ # @param [Symbol] name name of the method
41
+ # @param [Array] *args arguments
42
+ #
43
+
44
+ def method_missing(name, *args)
45
+ @batch.put(@target, name, args)
46
+ end
47
+
48
+ ##
49
+ # Executes the batch.
50
+ # @param [Proc] callback
51
+ #
52
+
53
+ def execute(&callback)
54
+ @batch.execute(&callback)
55
+ end
56
+
57
+ alias :"execute!" :execute
58
+
59
+ ##
60
+ # Returns all calls results.
61
+ # @return [Array]
62
+ #
63
+
64
+ def results
65
+ @batch.results
66
+ end
67
+ end
68
+
69
+ ##
70
+ # Batch of the objects and method names with arguments for runninng
71
+ # on a series.
72
+ #
73
+
74
+ class CallbackBatch
75
+
76
+ ##
77
+ # Holds the call stack of the ordered calls.
78
+ # @return [Array] the stack
79
+ #
80
+
81
+ attr_accessor :stack
82
+ @stack
83
+
84
+ ##
85
+ # Holds array with results of all calls.
86
+ # @return [Array]
87
+
88
+ attr_reader :results
89
+ @results
90
+
91
+ ##
92
+ # Constructor.
93
+ #
94
+
95
+ def initialize
96
+ @stack = [ ]
97
+ @results = [ ]
98
+ end
99
+
100
+ ##
101
+ # Puts call order to the batch. If block given, treat arguments
102
+ # as its arguments. In otherwise expects object, method name
103
+ # and arguments array.
104
+ #
105
+ # @param [Array] *args see above
106
+ # @param [Proc] &block see above
107
+ #
108
+
109
+ def put(*args, &block)
110
+ if block.nil?
111
+ object, method, args = args
112
+ args = [] if args.nil?
113
+ else
114
+ object = nil
115
+ method = block
116
+ end
117
+
118
+ @stack << [object, method, args]
119
+ end
120
+
121
+ ##
122
+ # Wraps object by method call receiver and catches all calls
123
+ # including arguments to the batch.
124
+ #
125
+ # @param [Object] target target object
126
+ # @return [EM::Batch::Call] an calls catcher
127
+ #
128
+
129
+ def take(target)
130
+ Call::new(self, target)
131
+ end
132
+
133
+ ##
134
+ # Executes the batch.
135
+ #
136
+ # @param [Proc] callback
137
+ # @yield result of last call
138
+ #
139
+
140
+ def execute(&callback)
141
+ caller = nil
142
+ result = nil
143
+
144
+ iterator = Proc::new do
145
+ object, method, args = @stack.shift
146
+ if object.nil? and method.kind_of? Proc
147
+ method.call(*args, &caller)
148
+ elsif not method.nil?
149
+ self.schedule_call do
150
+ if method == :exec
151
+ object.exec(*args, &caller)
152
+ else
153
+ object.send(method, *args, &caller)
154
+ end
155
+ end
156
+ elsif not callback.nil?
157
+ yield *result
158
+ end
159
+ end
160
+
161
+ caller = Proc::new do |*res|
162
+ self.schedule_tick do
163
+ if res.length == 1
164
+ result = res.first
165
+ else
166
+ result = res
167
+ end
168
+
169
+ @results << result
170
+ iterator.call()
171
+ end
172
+ end
173
+
174
+ iterator.call()
175
+ end
176
+
177
+ alias :"execute!" :execute
178
+
179
+ ##
180
+ # Schedules next tick execution.
181
+ # @yield
182
+ #
183
+
184
+ def schedule_tick
185
+ yield
186
+ end
187
+
188
+ ##
189
+ # Schedules next call execution.
190
+ # @yield
191
+ #
192
+
193
+ def schedule_call
194
+ yield
195
+ end
196
+ end
197
+
198
+ ##
199
+ # Calls catcher.
200
+ #
201
+
202
+ class CallbackBatch::Call
203
+
204
+ ##
205
+ # Holds parent batch object.
206
+ # @return [EM::Batch]
207
+ #
208
+
209
+ attr_reader :batch
210
+ @batch
211
+
212
+ ##
213
+ # Holds the wrapped object.
214
+ # @return [Object]
215
+ #
216
+
217
+ attr_accessor :target
218
+ @target
219
+
220
+ ##
221
+ # Constructor.
222
+ #
223
+ # @param [EM::Batch] batch parent batch
224
+ # @param [Object] target target object for wrap
225
+ #
226
+
227
+ def initialize(batch, target)
228
+ @batch = batch
229
+ @target = target
230
+ end
231
+
232
+ ##
233
+ # Missing methods catcher which puts the calls to parent batch.
234
+ #
235
+ # @param [Symbol] name method name
236
+ # @param [Array] *args method arguments list
237
+ #
238
+
239
+ def method_missing(name, *args)
240
+ @batch.put(@target, name, args)
241
+ end
242
+ end
data/test ADDED
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/ruby
2
+ # encoding: utf-8
3
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
4
+
5
+ $:.push("./lib")
6
+ $:.unshift("./lib")
7
+
8
+ require "callback-batch"
9
+ require "riot"
10
+
11
+ class Foo
12
+ def b1(&block)
13
+ yield :b1
14
+ end
15
+ def b2(&block)
16
+ yield :b2
17
+ end
18
+ end
19
+
20
+ context "CallbackBatch" do
21
+ asserts("#execute") do
22
+ q = CallbackBatch::new
23
+ f = Foo::new
24
+ q.take(f).b1
25
+ q.take(f).b2
26
+ q.execute!
27
+
28
+ q.results == [:b1, :b2]
29
+ end
30
+ end
31
+
32
+ context "CallbackSequence" do
33
+ asserts("#execute") do
34
+ q = CallbackSequencer::new(Foo::new)
35
+ q.b1
36
+ q.b2
37
+ q.execute!
38
+
39
+ q.results = [:b1, :b2]
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: callback-batch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Martin Kozák
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: &11537560 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *11537560
25
+ - !ruby/object:Gem::Dependency
26
+ name: jeweler
27
+ requirement: &11535840 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 1.5.2
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *11535840
36
+ - !ruby/object:Gem::Dependency
37
+ name: riot
38
+ requirement: &11514840 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 0.12.3
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *11514840
47
+ description:
48
+ email: martinkozak@martinkozak.net
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files:
52
+ - LICENSE.txt
53
+ - README.md
54
+ files:
55
+ - .document
56
+ - Gemfile
57
+ - Gemfile.lock
58
+ - LICENSE.txt
59
+ - README.md
60
+ - Rakefile
61
+ - VERSION
62
+ - callback-batch.gemspec
63
+ - lib/callback-batch.rb
64
+ - test
65
+ homepage: http://github.com/martinkozak/callback-batch
66
+ licenses:
67
+ - MIT
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ segments:
79
+ - 0
80
+ hash: 2991511423613802839
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 1.8.11
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Provides ability to run more callbacked methods of single or more objects
93
+ by elegant, readable and transparent way in a linear sequence, so subsequently in
94
+ single batch.
95
+ test_files: []