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.



42
43
44
# File 'lib/async_methods/async_methods.rb', line 42

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
31
32
33
34
35
36
37
38
39
# 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
    called_method = method[6 , method.length]
    if Thread.critical
      return send(called_method, *args, &block)
    else
      return Proxy.new(self, called_method, args, &block)
    end
  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)
    rescue Exception => e
      # Strip this method from the stack trace as it adds confusion
      e.backtrace.reject!{|line| line.include?(__FILE__)}
      raise e
    ensure
      stack.pop
    end
  end
end