Permalink
Browse files

refactor the __getattr__ into a mixin so we can use it in multiple pl…

…aces
  • Loading branch information...
1 parent 25041c4 commit 3ca950833f0b910bf5e756d2780d039d177437cd @dstufft dstufft committed Sep 8, 2011
Showing with 20 additions and 12 deletions.
  1. +20 −12 slumber/__init__.py
View
@@ -57,6 +57,25 @@ def __init__(self, *args, **kwargs):
super(MetaMixin, self).__init__(*args, **kwargs)
+class ResourceAttributesMixin(object):
+ """
+ A Mixin that makes it so that accessing an undefined attribute on a class
+ results in returning a Resource Instance. This Instance can then be used
+ to make calls to the a Resource.
+ """
+
+ def __getattr__(self, item):
+ try:
+ return self._meta.resources[item]
+ except KeyError:
+ self._meta.resources[item] = Resource(
+ base_url=urlparse.urljoin(self._meta.base_url, item),
+ format=self._meta.default_format,
+ authentication=self._meta.authentication
+ )
+ return self._meta.resources[item]
+
+
class Resource(MetaMixin, object):
"""
Resource provides the main functionality behind slumber. It handles the
@@ -197,7 +216,7 @@ def delete(self, **kwargs):
return False
-class API(MetaMixin, object):
+class API(ResourceAttributesMixin, MetaMixin, object):
class Meta:
resources = {}
@@ -221,14 +240,3 @@ def __init__(self, base_url=None, **kwargs):
if self._meta.authentication is not None:
self.http_client.add_credentials(**self._meta.authentication)
-
- def __getattr__(self, item):
- try:
- return self._meta.resources[item]
- except KeyError:
- self._meta.resources[item] = Resource(
- base_url=urlparse.urljoin(self._meta.base_url, item),
- format=self._meta.default_format,
- authentication=self._meta.authentication
- )
- return self._meta.resources[item]

0 comments on commit 3ca9508

Please sign in to comment.