aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2014-10-31 15:59:47 -0400
committerJesse Luehrs <doy@tozt.net>2014-10-31 15:59:47 -0400
commiteed0b063a3520c0c55c4e147789e4bbb53e55416 (patch)
tree9602e6ece9d56e07550e7f47a84ef8fb3a26d0c7
parent04d2eeb8414339856822d0543400c34de289fdde (diff)
downloadpython-mop-eed0b063a3520c0c55c4e147789e4bbb53e55416.tar.gz
python-mop-eed0b063a3520c0c55c4e147789e4bbb53e55416.zip
actually, looks like we do need to support defaults
otherwise Class's methods and attributes attributes won't be properly initialized
-rw-r--r--mop/__init__.py34
1 files changed, 29 insertions, 5 deletions
diff --git a/mop/__init__.py b/mop/__init__.py
index de649e4..554cc9c 100644
--- a/mop/__init__.py
+++ b/mop/__init__.py
@@ -62,11 +62,12 @@ def bootstrap():
}
)
- def bootstrap_create_attribute(name):
+ def bootstrap_create_attribute(name, default):
return BasicInstance(
globals().get("Attribute"),
{
"name": name,
+ "default": default,
}
)
@@ -154,12 +155,31 @@ def bootstrap():
"get_all_attributes", get_all_attributes
))
+ # get_default_for_instance requires get_default
+ Attribute.add_method(bootstrap_create_method(
+ "get_default", gen_reader("default")
+ ))
+
+ # create_instance requires get_default_for_instance
+ def get_default_for_instance(self):
+ default = self.get_default()
+ if callable(default):
+ default = default()
+ return default
+ Attribute.add_method(bootstrap_create_method(
+ "get_default_for_instance", get_default_for_instance
+ ))
+
# new requires create_instance
def create_instance(self, kwargs):
slots = {}
- for attr_name in self.get_all_attributes():
+ attrs = self.get_all_attributes()
+ for attr_name in attrs:
+ attr = attrs[attr_name]
if attr_name in kwargs.keys():
slots[attr_name] = kwargs[attr_name]
+ else:
+ slots[attr_name] = attr.get_default_for_instance()
instance = BasicInstance(self, slots)
instance.__class__ = python_class_for(self)
return instance
@@ -185,10 +205,14 @@ def bootstrap():
"add_attribute", add_attribute
))
- attr_name = bootstrap_create_attribute("name")
+ attr_name = bootstrap_create_attribute("name", None)
attr_name.__class__ = python_class_for(Attribute)
Attribute.add_attribute(attr_name)
+ attr_default = bootstrap_create_attribute("default", None)
+ attr_default.__class__ = python_class_for(Attribute)
+ Attribute.add_attribute(attr_default)
+
# and now object creation works! add the method attributes now to allow
# creating method objects
Method.add_attribute(Attribute.new(name="name"))
@@ -205,8 +229,8 @@ def bootstrap():
Class.add_attribute(Attribute.new(name="name"))
Class.add_attribute(Attribute.new(name="superclass"))
- Class.add_attribute(Attribute.new(name="attributes"))
- Class.add_attribute(Attribute.new(name="methods"))
+ Class.add_attribute(Attribute.new(name="attributes", default=lambda: {}))
+ Class.add_attribute(Attribute.new(name="methods", default=lambda: {}))
Class.add_method(Method.new(
name="get_name", body=gen_reader("name")