83. Users Born in 1980
UsersByDOB["1980-‐08-‐15"] = {
uuid_one: "koz"
}
84. Users Born in 1980
UsersByDOB["1980-‐08-‐15"] = {
uuid_one: "koz"
}
connection.get_range(:UsersByDOB, :start=>"1980", :finish=>"1981")
85. Users Born in 1980
UsersByDOB["1980-‐08-‐15"] = {
uuid_one: "koz"
}
connection.get_range(:UsersByDOB, :start=>"1980", :finish=>"1981")
Only with OrderPreservingPartitioner
101. class Customer < CassandraObject::Base
attribute :first_name, :type => :string
attribute :last_name, :type => :string
attribute :date_of_birth, :type => :date
attribute :signed_up_at, :type => :time_with_zone
validate :should_be_cool
key :uuid
index :date_of_birth
association :invoices, :unique=>false, :inverse_of=>:customer
private
def should_be_cool
unless ["Michael", "Anika", "Evan", "James"].include?(first_name)
errors.add(:first_name, "must be that of a cool person")
end
end
end
102. class Invoice < CassandraObject::Base
attribute :number, :type=>:integer
attribute :total, :type=>:float
attribute :gst_number, :type=>:string
# indexes can have a single entry also.
index :number, :unique=>true
# bi-‐directional associations with read-‐repair support.
association :customer, :unique=>true, :inverse_of=>:invoices
# Read migration support
migrate 1 do |attrs|
attrs["total"] ||= rand(2000) / 100.0
end
migrate 2 do |attrs|
attrs["gst_number"] = "66-‐666-‐666"
end
key :natural, :attributes => :number
end
105. Validations
validate :should_be_cool
def should_be_cool
unless ["Michael", "Anika", "Evan", "James"].include?(first_name)
errors.add(:first_name, "must be that of a cool person")
end
end
106. Validations
validate :should_be_cool
def should_be_cool
unless ["Michael", "Anika", "Evan", "James"].include?(first_name)
errors.add(:first_name, "must be that of a cool person")
end
end
@customer.first_name = "Marcel"
@customer.valid? # => false
115. Custom Key Factories
class RedisKeyFactory
def initialize(connection, key)
@connection, @key = connection, key
end
def next_key(object)
@connection.incr(@key)
end
# Parse should create a new key object from the 'to_param' format
def parse(string)
string.to_i
end
# create should create a new key object from the cassandra format.
def create(string)
string.to_i
end
end
117. Migrations
class AddLicenseNameToArticle < ActiveRecord::Migration
def self.up
add_column :articles, :license_name, :string, :default=>"Exclusive"
execute "UPDATE articles SET license_name = 'Exclusive'
WHERE price_first IS NOT NULL"
execute "UPDATE articles SET license_name = 'Syndicated'
WHERE price_first IS NULL"
end
def self.down
remove_column :articles, :license_name
end
end
119. Migrations
class Article < CassandraObject::Base
attribute :price_first, :type=>:float
attribute :license_name, :type=>:string
migrate 1 do |attrs|
if attrs[:price_first]
attrs[:license_name] = "Exclusive"
else
attrs[:license_name] = "Syndicated"
end
end
end
120. Migrations
class Article < CassandraObject::Base
attribute :price_first, :type=>:float
attribute :license_name, :type=>:string
migrate 1 do |attrs|
if attrs[:price_first]
attrs[:license_name] = "Exclusive"
else
attrs[:license_name] = "Syndicated"
end
end
end
@article = Article.get("some-‐old-‐story")
@article.license_name # => "Exclusive"
130. Associations
class Invoice < CassandraObject::Base
association :customer, :unique=>true, :inverse_of=>:invoices
end
class Customer < CassandraObject::Base
association :invoices, :unique=>false, :inverse_of=>:customer
end
@customer.invoices.create! params[:invoice]
@invoice.customer