Superstore
Cassandra Object uses ActiveModel to mimic much of the behavior in ActiveRecord.
Installation
Add the following to your Gemfile:
gem 'superstore'
Change the version of Cassandra accordingly. Recent versions have not been backward compatible.
Defining Models
class Widget < Superstore::Base
string :name
string :description
integer :price
array :colors, unique: true
validates :name, presence: :true
before_create do
self.description = "#{name} is the best product ever"
end
end
The table name defaults to the case-sensitive, pluralized name of the model class. To specify a
custom name, set the table_name
attribute on the class:
class MyWidget < Superstore::Base
table_name = 'my_widgets'
end
Using with Cassandra
Add the cassandra-cql gem to Gemfile:
gem 'cassandra-cql'
Add a config/superstore.yml:
development:
adapter: cassandra
keyspace: my_app_development
servers: 127.0.0.1:9160
thrift:
timeout: 20
retries: 2
Using with Postgres HStore
Add the pg gem to your Gemfile:
gem 'pg'
And a config/superstore.yml:
development:
adapter: hstore
Creating and updating records
Cassandra Object has equivalent methods as ActiveRecord:
= Widget.new
.valid?
= Widget.create(name: 'Acme', price: 100)
.update_attribute(:price, 1200)
.update(price: 1200, name: 'Acme Corporation')
.attributes = {price: 300}
.price_was
.save
.save!
Finding records
= Widget.find(uuid)
= Widget.first
= Widget.all
Widget.find_each do ||
# Codez
end
Scoping
Some lightweight scoping features are available:
Widget.where('color' => 'red')
Widget.select(['name', 'color'])
Widget.limit(10)