Class: CallbackBatch

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeCallbackBatch

Constructor.



95
96
97
98
# File 'lib/callback-batch.rb', line 95

def initialize
    @stack = [ ]
    @results = [ ]
end

Instance Attribute Details

#resultsArray (readonly)

Holds array with results of all calls.

Returns:

  • (Array)


88
89
90
# File 'lib/callback-batch.rb', line 88

def results
  @results
end

#stackArray

Holds the call stack of the ordered calls.

Returns:

  • (Array)

    the stack



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.

Parameters:

  • callback (Proc)

Yields:

  • result of last call



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.

Parameters:

  • *args (Array)

    see above

  • &block (Proc)

    see above



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.

Yields:



193
194
195
# File 'lib/callback-batch.rb', line 193

def schedule_call
    yield
end

#schedule_tick { ... } ⇒ Object

Schedules next tick execution.

Yields:



184
185
186
# File 'lib/callback-batch.rb', line 184

def schedule_tick
    yield
end

#take(target) ⇒ EM::Batch::Call

Wraps object by method call receiver and catches all calls including arguments to the batch.

Parameters:

  • target (Object)

    target object

Returns:

  • (EM::Batch::Call)

    an calls catcher



129
130
131
# File 'lib/callback-batch.rb', line 129

def take(target)
    Call::new(self, target)
end