Created
November 12, 2011 06:34
-
-
Save cjameshuff/1360138 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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