Skip to content

Instantly share code, notes, and snippets.

@cjameshuff
Created November 12, 2011 06:34
Show Gist options
  • Save cjameshuff/1360138 to your computer and use it in GitHub Desktop.
Save cjameshuff/1360138 to your computer and use it in GitHub Desktop.
class ThreadmappedObject
def initialize(prototype, delegates = {})
@delegates = delegates
prototype.methods.each {|m|
(class << self; self; end).class_eval {
define_method(m) {|*args, &block|
deleg = @delegates.fetch(Thread.current) {@delegates[:default]}
if(deleg)
deleg.send(m, *args, &block)
else
raise "No object mapped for thread"
end
}
}
}
end
def threadmap(obj, thread = nil)
if(thread)
@delegates[thread] = obj
else
@delegates[Thread.current] = obj
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment