Created
June 16, 2014 16:13
-
-
Save samuraisam/8e755073adf83eff7bbc to your computer and use it in GitHub Desktop.
Python: Fluent Builder Pattern
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 ObjectPermissions(object): | |
perms_map = {} | |
specific_object_perms = () | |
@classmethod | |
def require(cls, *new_perms): | |
class SpecificObjectPermissions(cls): | |
specific_object_perms = new_perms | |
return SpecificObjectPermissions | |
@classmethod | |
def methods(cls, **methods): | |
class SpecificObjectPermissions(cls): | |
perms_map = copy.deepcopy(cls.perms_map) | |
SpecificObjectPermissions.perms_map.update(methods) | |
return SpecificObjectPermissions | |
### USAGE | |
# this can stick around however long you want, or make it 1 use | |
PermsClass = ObjectPermissions.require('delete', 'invite').methods(POST=['create_user']) | |
# create an instance | |
perms = PermsClass() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment