limiting resource lazyloading to a single query

This commit is contained in:
Brian Waldon 2011-08-29 16:33:22 -04:00
parent 5969022401
commit 39068edfb7
2 changed files with 8 additions and 2 deletions

View File

@ -204,14 +204,21 @@ class Resource(object):
self.manager = manager
self._info = info
self._add_details(info)
self._loaded = False
def _add_details(self, info):
for (k, v) in info.iteritems():
setattr(self, k, v)
def __getattr__(self, k):
self.get()
if k not in self.__dict__:
#NOTE(bcwaldon): allow a resource to try to init itself only once
if not self._loaded:
self._loaded = True
self.get()
return self.__getattr__(k)
raise AttributeError(k)
else:
return self.__dict__[k]

View File

@ -30,7 +30,6 @@ class BaseTest(utils.TestCase):
# Missing stuff still fails after a second get
self.assertRaises(AttributeError, getattr, f, 'blahblah')
cs.assert_called('GET', '/flavors/1')
def test_eq(self):
# Two resources of the same type with the same id: equal