Class: CallbackBatch
- Inherits:
-
Object
- Object
- CallbackBatch
- Defined in:
- lib/callback-batch.rb
Overview
Batch of the objects and method names with arguments for runninng on a series.
Defined Under Namespace
Classes: Call
Instance Attribute Summary collapse
-
#results ⇒ Array
readonly
Holds array with results of all calls.
-
#stack ⇒ Array
Holds the call stack of the ordered calls.
Instance Method Summary collapse
-
#execute(&callback) { ... } ⇒ Object
(also: #execute!)
Executes the batch.
-
#initialize ⇒ CallbackBatch
constructor
Constructor.
-
#put(*args, &block) ⇒ Object
Puts call order to the batch.
-
#schedule_call { ... } ⇒ Object
Schedules next call execution.
-
#schedule_tick { ... } ⇒ Object
Schedules next tick execution.
-
#take(target) ⇒ EM::Batch::Call
Wraps object by method call receiver and catches all calls including arguments to the batch.
Constructor Details
#initialize ⇒ CallbackBatch
Constructor.
95 96 97 98 |
# File 'lib/callback-batch.rb', line 95 def initialize @stack = [ ] @results = [ ] end |
Instance Attribute Details
#results ⇒ Array (readonly)
Holds array with results of all calls.
88 89 90 |
# File 'lib/callback-batch.rb', line 88 def results @results end |
#stack ⇒ Array
Holds the call stack of the ordered calls.
81 82 83 |
# File 'lib/callback-batch.rb', line 81 def stack @stack end |
Instance Method Details
#execute(&callback) { ... } ⇒ Object Also known as: execute!
Executes the batch.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/callback-batch.rb', line 140 def execute(&callback) caller = nil result = nil iterator = Proc::new do object, method, args = @stack.shift if object.nil? and method.kind_of? Proc method.call(*args, &caller) elsif not method.nil? self.schedule_call do if method == :exec object.exec(*args, &caller) else object.send(method, *args, &caller) end end elsif not callback.nil? yield *result end end caller = Proc::new do |*res| self.schedule_tick do if res.length == 1 result = res.first else result = res end @results << result iterator.call() end end iterator.call() end |
#put(*args, &block) ⇒ Object
Puts call order to the batch. If block given, treat arguments as its arguments. In otherwise expects object, method name and arguments array.
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/callback-batch.rb', line 109 def put(*args, &block) if block.nil? object, method, args = args args = [] if args.nil? else object = nil method = block end @stack << [object, method, args] end |
#schedule_call { ... } ⇒ Object
Schedules next call execution.
193 194 195 |
# File 'lib/callback-batch.rb', line 193 def schedule_call yield end |
#schedule_tick { ... } ⇒ Object
Schedules next tick execution.
184 185 186 |
# File 'lib/callback-batch.rb', line 184 def schedule_tick yield end |