From 5c770cb1918d8067db69d3c880a9a6ad1d31ddb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Tue, 6 Sep 2011 12:04:51 +0200 Subject: Huge refactoring and code cleanup. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Raphaƫl Barrois --- factory/declarations.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'factory/declarations.py') diff --git a/factory/declarations.py b/factory/declarations.py index 898493c..a1e9102 100644 --- a/factory/declarations.py +++ b/factory/declarations.py @@ -48,22 +48,24 @@ class OrderedDeclaration(object): def __init__(self): self.order = GlobalCounter.step() - def evaluate(self, factory, attributes): + def evaluate(self, factory, obj): """Evaluate this declaration. Args: factory: The factory this declaration was defined in. + obj: The object holding currently computed attributes attributes: The attributes created by the unordered and ordered declarations up to this point.""" raise NotImplementedError('This is an abstract method') + class LazyAttribute(OrderedDeclaration): def __init__(self, function): super(LazyAttribute, self).__init__() self.function = function - def evaluate(self, factory, attributes): - return self.function(attributes) + def evaluate(self, factory, obj): + return self.function(obj) class SelfAttribute(OrderedDeclaration): @@ -71,8 +73,8 @@ class SelfAttribute(OrderedDeclaration): super(SelfAttribute, self).__init__() self.attribute_name = attribute_name - def evaluate(self, factory, attributes): - return getattr(attributes, self.attribute_name) + def evaluate(self, factory, obj): + return getattr(obj, self.attribute_name) class Sequence(OrderedDeclaration): @@ -81,12 +83,14 @@ class Sequence(OrderedDeclaration): self.function = function self.type = type - def evaluate(self, factory, attributes): + def evaluate(self, factory, obj): return self.function(self.type(factory.sequence)) + class LazyAttributeSequence(Sequence): - def evaluate(self, factory, attributes): - return self.function(attributes, self.type(factory.sequence)) + def evaluate(self, factory, obj): + return self.function(obj, self.type(factory.sequence)) + class SubFactory(OrderedDeclaration): """Base class for attributes based upon a sub-factory. @@ -98,20 +102,24 @@ class SubFactory(OrderedDeclaration): def __init__(self, factory, **kwargs): super(SubFactory, self).__init__() - self.defaults = factory.declarations() - self.defaults.update_base(kwargs) + self.defaults = kwargs self.factory = factory - def evaluate(self, factory, create, attributes): + def evaluate(self, factory, create, extra): """Evaluate the current definition and fill its attributes. Uses attributes definition in the following order: - attributes defined in the wrapped factory class - values defined when defining the SubFactory - - additional valued defined in attributes + - additional values defined in attributes """ - attrs = self.defaults.build_attributes(self.factory, create, attributes) + defaults = dict(self.defaults) + if extra: + defaults.update(extra) + + attrs = self.factory.attributes(create, defaults) + if create: return self.factory.create(**attrs) else: -- cgit v1.2.3