Module: AsyncMethods::InstanceMethods

Defined in:
lib/async_methods/async_methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
# File 'lib/async_methods/async_methods.rb', line 7

def self.included (base)
  base.send :alias_method, :method_missing_without_async, :method_missing
  base.send :alias_method, :method_missing, :method_missing_with_async
end

Instance Method Details

#asynchronous_block(&block) ⇒ Object

Call a block asynchronously.



33
34
35
# File 'lib/async_methods/async_methods.rb', line 33

def asynchronous_block (&block)
  Proxy.new(nil, nil, nil, &block)
end

#method_missing_with_async(method, *args, &block) ⇒ Object

Override missing method to add the async method handling



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/async_methods/async_methods.rb', line 13

def method_missing_with_async (method, *args, &block)
  method = method.to_s
  if method[0, 6] == 'async_'
    method = method.to_s
    return Proxy.new(self, method[6 , method.length], args, &block)
  else
    # Keep track of the current missing method calls to keep out of an infinite loop
    stack = Thread.current[:async_method_missing_methods] ||= []
    sig = MethodSignature.new(self, method)
    raise NoMethodError.new("undefined method `#{method}' for #{self.inspect}") if stack.include?(sig)
    begin
      stack.push(sig)
      return method_missing_without_async(method, *args, &block)
    ensure
      stack.pop
    end
  end
end