Commit 9762e67c authored by Eliot Berriot's avatar Eliot Berriot

Added prepare_data hook for manipulating data before registration

parent 55400e18
......@@ -67,10 +67,16 @@ class Registry(OrderedDict):
if self.validate(obj):
if name is None:
name = self.get_object_name(obj)
self[name] = obj
self[name] = self.prepare_data(obj)
else:
raise ValueError("{0} (type: {0.__class__}) is not a valid value for {1} registry".format(obj, self.__class__))
def prepare_data(self, obj):
"""
Override this methode if you want to manipulate data before registering it
"""
return obj
def autodiscover(self, apps, force_reload=True):
"""
Iterate throught every installed apps, trying to import `look_into` package
......
......@@ -110,6 +110,19 @@ class RegistryTest(unittest.TestCase):
self.assertNotEqual(registry.get('Potato', None), None)
self.assertNotEqual(registry.get('Ketchup', None), None)
def test_can_manipulate_data_before_registering(self):
class ModifyData(registries.Registry):
def prepare_data(self, obj):
return "hello " + obj
r = ModifyData()
r.register("eliot", name="eliot")
r.register("roger", name="roger")
self.assertEqual(r.get('eliot'), "hello eliot")
self.assertEqual(r.get('roger'), "hello roger")
if __name__ == '__main__':
unittest.main()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment