Skip to content

Instantly share code, notes, and snippets.

@joestump
Created September 1, 2009 21:11
Show Gist options
  • Save joestump/179397 to your computer and use it in GitHub Desktop.
Save joestump/179397 to your computer and use it in GitHub Desktop.
class Model(object):
keyspace = None
column_family = None
key = None
data = {}
required = []
modified = False
def __init__(self, **kwargs):
if self.key in kwargs and len(kwargs) == 1:
self.data = Config.driver.get(
keyspace=self.keyspace, column_family=self.column_family,
key=kwargs[self.key]
)
else:
self.data = kwargs
def __setattr__(self, column, value):
self.modified = True
self.data[column] = value
def __getattr__(self, column):
return self.data[column]
def __delattr__(self, column):
self.modified = True
del self.data[column]
def save(self):
if self.modified is False:
return
if self.keyspace is None or self.column_family is None or \
self.key is None:
raise ValueError('Missing required data')
for req in required:
if req not in self.data:
raise ValueError('Required field missing: %s' % req)
Config.driver.insert(
keyspace=self.keyspace, column_family=self.column_family,
record=self.data, key=self.data[self.key]
)
self.modified = False
def delete(self):
Config.driver.remove(
keyspace=self.keyspace, key=self.data[self.key],
column_family=self.column_family
)
self.data = {}
self.modified = False
class Layer(object):
keyspace = 'Layers'
column_family = 'Layer'
required = ['name', 'owner', 'public']
key = 'name'
class Token(object):
keyspace = 'Tokens'
column_family = 'Token'
required = ['owner', 'token', 'secret']
key = 'token'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment