from roleplay import does, has_role from roleplay.role import Role class Base(Role): def base(self): return "Base.base" class Foo(Role): def foo(self): return "foo.foo" has_role(Foo, Base) assert does(Foo, Base) class Bar(Role): def bar(self): return "Bar.bar" has_role(Bar, Foo) assert does(Bar, Foo) assert does(Bar, Base) class Fubar(Role): def fubar(self):
from roleplay import does, has_role from roleplay.role import Role class Base(Role): def base(self): return "Base.base" class Foo(Role): def foo(self): return "foo.foo" has_role(Foo, Base); assert does(Foo, Base); class Bar(Role): def bar(self): return "Bar.bar" has_role(Bar, Foo) assert does(Bar, Foo) assert does(Bar, Base) class Fubar(Role): def fubar(self): return "Fubar.fubar" import inspect print inspect.getframeinfo(inspect.currentframe())[0].ljust(40) + ": OK! All tests passed."
class sleeper(Role): def sleep(self): return 'snore' def talk(self): return 'zzz' class Class(object): def sleep(self): return 'nite-nite' assert not hasattr(Class, 'talk') # The role is not composed at the class level assert not hasattr(Class(), 'talk') # The role is not composed at the object level obj = Class() has_role(obj, bark) assert not hasattr(Class, 'talk') assert hasattr(obj, 'talk') obj = Class() assert obj.sleep() == "nite-nite" has_role(obj, sleeper) assert hasattr(obj, 'sleep') assert hasattr(obj, 'talk') assert obj.sleep() == 'snore' # The role silently overrides the class method assert obj.talk() == 'zzz' # The role silently overrides the role's method import inspect print inspect.getframeinfo(inspect.currentframe())[0].ljust(40) + ": OK! All tests passed."
# Create 2 roles class rFoo(Role): def foo(self): return "rFoo::foo" class rBar(Role): pass # Combine them into a third role class rFooBar(Role): pass has_role(rFooBar, rFoo, rBar) import inspect assert Role in inspect.getmro(rFoo) assert Role in inspect.getmro(rBar) assert Role in inspect.getmro(rFooBar) assert rFoo not in inspect.getmro(rFooBar) assert rBar not in inspect.getmro(rFooBar) assert does(rFoo, rFoo) assert does(rBar, rBar) assert does(rFooBar, rFoo) assert does(rFooBar, rBar) assert does(rFooBar, rFooBar) # create another role
def foo(self): return "rFoo::foo" def use_sleeping(self, other): print str(self) return other.sleep() class FooBar(object): pass raises_on_missing_requirement = 0 try: has_role(FooBar, rFoo) except ClassDoesNotFulfillRequirement: raises_on_missing_requirement = 1 assert raises_on_missing_requirement class FooBarComplete(object): def sleep(self): return "FooBarComplete.sleep" no_raise_on_complete = 1 try: has_role(FooBarComplete, rFoo) except ClassDoesNotFulfillRequirement: no_raise_on_complete = 0
# $Id$ # $Source$ # $Author$ # $HeadURL$ # $Revision$ # $Date$ ''' Example class using the revision role ''' from roleplay import does, has_role from examples.revisions import RevisionRole class MyClass(object): table = "foo" primary_key = 'id' def hello(self): return self.save_revision() has_role(MyClass, RevisionRole) obj = MyClass() obj.hello()
from roleplay.role import ClassDoesNotFulfillRequirement class rFoo(Role): __requires__ = ["sleep"] def foo(self): return "rFoo::foo" def use_sleeping(self, other): print str(self) return other.sleep() class FooBar(object): pass raises_on_missing_requirement = 0 try: has_role(FooBar, rFoo) except ClassDoesNotFulfillRequirement: raises_on_missing_requirement = 1 assert raises_on_missing_requirement class FooBarComplete(object): def sleep(self): return "FooBarComplete.sleep" no_raise_on_complete = 1 try: has_role(FooBarComplete, rFoo) except ClassDoesNotFulfillRequirement: no_raise_on_complete = 0 assert no_raise_on_complete