Skip to content

Instantly share code, notes, and snippets.

@cyberfox
Created November 9, 2009 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyberfox/230438 to your computer and use it in GitHub Desktop.
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 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