Skip to content

Instantly share code, notes, and snippets.

@tomafro
Created March 4, 2010 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomafro/321584 to your computer and use it in GitHub Desktop.
Save tomafro/321584 to your computer and use it in GitHub Desktop.
class Pumphouse::Model
extend ActiveModel::Naming
def self.connection=(*args)
@connection = Cassandra.new(*args)
end
def self.connection
@connection
end
def self.load(key)
attributes = Pumphouse::Model.connection.get(model_name, key)
instantiate(key, attributes) if attributes
end
def initialize(attributes)
@attributes = attributes
@key = UUID.new.to_guid
end
def self.instantiate(key, attributes)
returning allocate do |object|
object.instance_variable_set("@key", key)
object.instance_variable_set("@attributes", attributes)
end
end
def attributes
@attributes
end
def save
returning @key do
Pumphouse::Model.connection.insert(self.class.model_name, @key.to_s, attributes, {})
end
end
end
Pumphouse::Model.connection = 'Pumphouse'
class Vegetable < Pumphouse::Model
end
key = Vegetable.new(:name => 'Aubergine').save
Vegetable.find(key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment