Permalink
Browse files

Add support for passing keyword args upon initialization

This enables creating and instance 'on-the-fly' without
needing to subclass for simple customization.
  • Loading branch information...
1 parent ed8d8f3 commit d5860e5bb337e1c6094c32dd9e39980f9ec7697a @bruth committed Jan 2, 2013
Showing with 9 additions and 6 deletions.
  1. +9 −6 restlib2/resources.py
View
@@ -234,6 +234,15 @@ class Resource(object):
# - http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4
cache_must_revalidate = False
+ def __init__(self, **kwargs):
+ for key in kwargs:
+ # Not methods nor methods
+ if key in self.allowed_methods or callable(getattr(self, key, None)):
+ raise TypeError('No HTTP handlers nor methods can be overriden.')
+ if not hasattr(self, key):
+ raise TypeError('{0} is not a valid keyword argument for this resource.'.format(key))
+ setattr(self, key, kwargs[key])
+
# ## Initialize Once, Process Many
# Every `Resource` class can be initialized once since they are stateless
# (and thus thread-safe).
@@ -764,9 +773,3 @@ def process_response(self, request, response):
self.set_last_modified(request, response)
return response
-
- # ## Request Programatically
- # For composite resources, `resource.apply` can be used on related resources
- # with the original `request`.
- def apply(self, request, *args, **kargs):
- pass

0 comments on commit d5860e5

Please sign in to comment.