Permalink
Browse files

entities now store relations in uow

  • Loading branch information...
1 parent fce0bd8 commit 2e696e6b8b41817068e85e4c0830f3471ac5db60 @srobertson committed Apr 4, 2012
@@ -71,9 +71,18 @@ def lazy_map_all(records):
return map(lambda r: model(**r), records)
op.block = lazy_map_all
- return op
- #attributes = cls.storage_by_class[model][retrieval]
- #return model(**attributes)
+ return op
+
+ @classmethod
+ @coroutine
+ def find_related(cls, entity, relation, *args, **conditions):
+ # TODO: this should mimic the entity.find behavior
+
+ if relation.cardinality == 'many':
+ yield entity.attr.get(relation.name,{}).values
+ else:
+ yield entity.attr.get(relation.name)
+
@classmethod
@@ -62,11 +62,7 @@ def __set__(self, obj, value):
obj.attr[self.name] = value
inverse = self.inverse
if inverse:
- value.attr[inverse.name] = obj
-
- #op = obj.relate(value, self)
- #op = value.relate(obj,self)
- #self.scheduler.queue.add_operation(op)
+ setattr(value, inverse.name, obj)
@property
def destination(self):
@@ -100,13 +96,7 @@ def guess_inverse(self):
singular = self.model.__name__.lower()
return (singular, self.en_inflector.pluralize(singular))
-
- #@property
- #def model(self):
- # if self._model is None:
- # self._model = self.comp_reg.lookup(self.model_name)
- # return self._model
-
+
class many(one):
cardinality = "many"
def wrap(self, obj):
@@ -139,9 +129,13 @@ def create(self, *args, **kw):
entity = yield self.destination.create(*args, **kw)
yield self.set(entity)
yield entity
-
+
+ @coroutine
def find(self):
- return self.destination.find_related(self.obj, self.relation)
+ if self.obj.attr.has_key(self.relation.name):
+ yield self.obj.attr[self.relation.name]
+ else:
+ yield self.destination.find_related(self.obj, self.relation)
def set(self, other):
return other.relate(self.obj, self.relation)
@@ -176,7 +170,8 @@ def add(self, obj):
#obj[self.foreign_key] = self.obj.primary_key
# Return the op incase some one has the urge to wait for it
#return obj.save()
- return self.obj.relate(obj, self.relation)
+ self.values.add(obj)
+ #return self.obj.relate(obj, self.relation)
def create(self, **kw):
op = self.relation.destination.create_related(self.obj, self.relation, **kw)
@@ -144,6 +144,8 @@ def clear(self):
self._obj_states = {}
rollback = clear
+
+
def objects(self):
# flatten all objects
return list(itertools.chain(*self._objects.values()))
@@ -1,4 +1,4 @@
-from nose.tools import eq_
+from nose.tools import eq_, assert_raises
import os
from Rambler import field, coroutine
@@ -55,23 +55,26 @@ def routine():
eq_(len(uow.get_clean()), 1)
minion1 = yield self.Employee.create(name="El Hefe", manager=manager)
- eq_(minion1.manager, manager)
- assert minion1 in manager.subordinates
+
+ minion1_manager = yield minion1.manager()
+
+ eq_(minion1_manager, manager)
+
+ subordinates = yield manager.subordinates()
+ assert minion1 in subordinates
eq_(len(uow.objects()), 2)
eq_(len(uow.get_new()), 1)
eq_(len(uow.get_clean()), 1)
- # forget throws an error if their are changed objects
- assert_raises(RuntimeError, uow.forget)
yield self.Employee.commit()
eq_(len(uow.objects()), 2)
eq_(len(uow.get_new()), 0)
eq_(len(uow.get_clean()), 2)
- uow.forget()
+ uow.clear()
eq_(len(uow.objects()), 0)
eq_(len(uow.get_new()), 0)
eq_(len(uow.get_clean()), 0)

0 comments on commit 2e696e6

Please sign in to comment.