Created
November 9, 2009 23:35
-
-
Save cyberfox/230438 to your computer and use it in GitHub Desktop.
Make ActiveResource able to handle authentication w/o acting like a global variable.
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
# This is a gross hack to make it possible to access the same | |
# resource from the context of different users at the same time. | |
# I'm quite positive this is wasteful and bad, but it's the only | |
# way to 'de-global' subclasses of ActiveResource::Base. :( It's | |
# also possible it leaks memory if the GC doesn't collect Class | |
# objects. -- Morgan Schweers, 09-Nov-2009 | |
class ResourceFactory | |
def self.get(klass) | |
Class.new(klass) do |resource| | |
resource.collection_name = klass.collection_name | |
resource.element_name = klass.element_name | |
resource.format = klass.format | |
end | |
end | |
end | |
# Usage (presuming a Message class exists, descended from ActiveResource::Base): | |
# from = ResourceFactory.get(Message) | |
# from.user='morgan' | |
# | |
# to = ResourceFactory.get(Message) | |
# to.user='corwin' | |
# | |
# to.find(1) | |
# from.find(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment