Module: Cassie::Model
- Extended by:
- ActiveModel::Callbacks, ActiveSupport::Concern
- Includes:
- ActiveModel::Model, ActiveModel::Validations, ActiveModel::Validations::Callbacks
- Defined in:
- lib/cassie/model.rb
Overview
This module provides a simple interface for models backed by Cassandra tables.
Cassandra is very limited in how data can be accessed efficiently so this code is intentionally not designed as a full fledged DSL with all the nifty features of ActiveRecord. Doing so will only get you into trouble when you run into the limits of Cassandra data structures.
It implements ActiveModel::Model and supports ActiveModel callbacks on :create, :update, :save, and :destroy as well as ActiveModel validations.
Example:
class Thing
include Cassie::Model
self.table_name = "things"
self.keyspace = "test"
self.primary_key = [:owner, :id]
column :owner, :int
column :id, :int, :as => :identifier
column :val, :varchar, :as => :value
ordering_key :id, :desc
validates_presence_of :id, :value
before_save do
...
end
end
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#attributes ⇒ Object
Returns a hash of column to values.
-
#destroy ⇒ Object
Delete a record and call the destroy callbacks.
- #eql?(other) ⇒ Boolean
- #initialize(attributes = {}) ⇒ Object
-
#key_hash ⇒ Object
Returns the primary key as a hash.
-
#persisted? ⇒ Boolean
Return true if the record has been persisted to Cassandra.
-
#persistence_ttl ⇒ Object
Subclasses can override this method to provide a TTL on the persisted record.
-
#save(validate: true, ttl: nil) ⇒ Object
Save a record.
-
#save! ⇒ Object
Save a record.
Instance Method Details
#==(other) ⇒ Object
459 460 461 |
# File 'lib/cassie/model.rb', line 459 def ==(other) eql?(other) end |
#attributes ⇒ Object
Returns a hash of column to values. Column names will be symbols.
442 443 444 445 446 447 448 |
# File 'lib/cassie/model.rb', line 442 def attributes hash = {} self.class.column_names.each do |name| hash[name] = send(name) end hash end |
#destroy ⇒ Object
Delete a record and call the destroy callbacks.
433 434 435 436 437 438 439 |
# File 'lib/cassie/model.rb', line 433 def destroy run_callbacks(:destroy) do self.class.connection.delete(self.class.full_table_name, key_hash) @persisted = false true end end |
#eql?(other) ⇒ Boolean
455 456 457 |
# File 'lib/cassie/model.rb', line 455 def eql?(other) other.is_a?(self.class) && other.key_hash == key_hash end |
#initialize(attributes = {}) ⇒ Object
388 389 390 391 |
# File 'lib/cassie/model.rb', line 388 def initialize(attributes = {}) super @persisted = false end |
#key_hash ⇒ Object
Returns the primary key as a hash
464 465 466 467 468 469 470 |
# File 'lib/cassie/model.rb', line 464 def key_hash hash = {} self.class.primary_key.each do |key| hash[key] = self.send(key) end hash end |
#persisted? ⇒ Boolean
Return true if the record has been persisted to Cassandra.
394 395 396 |
# File 'lib/cassie/model.rb', line 394 def persisted? @persisted end |
#persistence_ttl ⇒ Object
Subclasses can override this method to provide a TTL on the persisted record.
451 452 453 |
# File 'lib/cassie/model.rb', line 451 def persistence_ttl nil end |
#save(validate: true, ttl: nil) ⇒ Object
Save a record. Returns true if the record was persisted and false if it was invalid. This method will run the save callbacks as well as either the update or create callbacks as necessary.
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/cassie/model.rb', line 401 def save(validate: true, ttl: nil) valid_record = (validate ? valid? : true) if valid_record run_callbacks(:save) do if persisted? run_callbacks(:update) do self.class.connection.update(self.class.full_table_name, values_hash, key_hash, :ttl => persistence_ttl || ttl) end else run_callbacks(:create) do self.class.connection.insert(self.class.full_table_name, attributes, :ttl => persistence_ttl || ttl) @persisted = true end end end true else false end end |
#save! ⇒ Object
Save a record. Returns true if the record was saved and raises an ActiveRecord::RecordInvalid error if the record is invalid.
424 425 426 427 428 429 430 |
# File 'lib/cassie/model.rb', line 424 def save! if save true else raise Cassie::RecordInvalid.new(self) end end |