Skip to content

Instantly share code, notes, and snippets.

@samuraisam
Created June 16, 2014 16:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samuraisam/8e755073adf83eff7bbc to your computer and use it in GitHub Desktop.
Save samuraisam/8e755073adf83eff7bbc to your computer and use it in GitHub Desktop.
Python: Fluent Builder Pattern
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