Skip to content

Instantly share code, notes, and snippets.

@cararemixed
Created April 19, 2012 20:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cararemixed/2423960 to your computer and use it in GitHub Desktop.
Save cararemixed/2423960 to your computer and use it in GitHub Desktop.
An experimental Celluloid::ActorProxy patch.
module Celluloid
class ActorProxy
private
def def_async(method)
impl = ->(*a, &b) {Actor.async @mailbox, method, *a, &b}
ActorProxy.class_eval {define_method("#{method}!", &impl)}
impl
end
def def_sync(method)
impl = ->(*a, &b) {Actor.call @mailbox, method, *a, &b}
ActorProxy.class_eval {define_method(method, &impl)}
impl
end
# method_missing black magic to call bang predicate methods asynchronously
def method_missing(method, *args, &block)
# bang methods are async calls
if method.match(/(.*)!$/)
def_async($1).call(*args, &block)
else
def_sync(method).call(*args, &block)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment