Created
July 12, 2013 14:30
-
-
Save podhmo/5984902 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var core = { | |
name: "foo", | |
published: false | |
}; | |
var wrapped = { | |
useful_method: function(){return "userful";} | |
} | |
wrapped.__proto__ = core; | |
var assert = function asert(p){ | |
if (!p()){ | |
throw "error"; | |
} | |
} | |
//assert(function(){return false}); | |
assert(function(){return wrapped.name == "foo"}); | |
assert(function(){return wrapped.published == false}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CoreModel(object): | |
def __init__(self, name, published=False): | |
self.name = name | |
self.published = published | |
def publish(self): | |
self.publish = True | |
def unpublish(self): | |
self.publish = False | |
class WrappedModel1(object): | |
def __init__(self, core): | |
self.core = core | |
## delegation | |
@property | |
def name(self): | |
return self.core.name | |
@property | |
def published(self): | |
return self.core.published | |
def useful_method(self, *args, **kwargs): | |
print "useful!" | |
def delegate_field(attr, fields): | |
def _delegate_field(cls): | |
for f in fields: | |
setattr(cls, f, property(lambda self, k=f: getattr(getattr(self, attr), k))) | |
return cls | |
return _delegate_field | |
@delegate_field("core", ["name", "published"]) | |
class WrappedModel2(object): | |
def __init__(self, core): | |
self.core = core | |
def useful_method(self, *args, **kwargs): | |
print "useful!" | |
class DelegationMeta(type): | |
def __new__(cls, name, bases, attrs): | |
if "Delegation" in attrs: | |
Delegation = attrs["Delegation"] | |
target = Delegation.target | |
for fs in Delegation.fields: | |
if not isinstance(fs, (tuple, list)): | |
def access(self, k=fs): | |
return getattr(getattr(self, target), k) | |
f, accessor = fs, access | |
else: | |
f, accessor = fs | |
attrs[f] = property(accessor) | |
return type.__new__(cls, name, bases, attrs) | |
class WrappedModel3(object): | |
__metaclass__ = DelegationMeta | |
class Delegation: | |
target = "core" | |
fields = ["name", "published"] | |
def __init__(self, core): | |
self.core = core | |
class WrappedModel4(object): | |
__metaclass__ = DelegationMeta | |
class Delegation: | |
target = "core" | |
fields = [("name", lambda self: self.core.name * 2), "published"] | |
def __init__(self, core): | |
self.core = core | |
wm = WrappedModel1(CoreModel("foo")) | |
assert wm.name == "foo" | |
assert wm.published == False | |
wm2 = WrappedModel2(CoreModel("foo")) | |
assert wm2.name == "foo" | |
assert wm2.published == False | |
wm3 = WrappedModel3(CoreModel("foo")) | |
assert wm3.name == "foo" | |
assert wm3.published == False | |
wm4 = WrappedModel4(CoreModel("foo")) | |
assert wm4.name == "foofoo" | |
assert wm4.published == False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment