Module: Wukong::Store::CassandraModel

Defined in:
lib/wukong/store/cassandra_model.rb,
lib/wukong/store/cassandra/streaming.rb

Overview

Barebones interface between a wukong class and a cassandra database

Class must somehow provide a class-level cassandra_db accessor that sets the @cassandra_db instance variable.

Defined Under Namespace

Modules: ClassMethods Classes: AvroWriter

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) included(base)

The standard 'inject class methods when module is included' trick



67
68
69
# File 'lib/wukong/store/cassandra_model.rb', line 67

def self.included base
  base.class_eval{ extend ClassMethods}
end

Instance Method Details

- (Object) save

Store model to the DB



13
14
15
# File 'lib/wukong/store/cassandra_model.rb', line 13

def save
  self.class.insert key, self.to_db_hash
end

- (Object) streaming_save

Store model using avro writer



9
10
11
# File 'lib/wukong/store/cassandra/streaming.rb', line 9

def streaming_save
  self.class.streaming_insert id, self
end

- (Object) to_db_hash

Flatten attributes for storage in the DB.

  • omits elements whose value is nil

  • calls to_s on everything else

  • This means that blank strings are preserved;

  • and that false is saved as 'false'

Override if you think something fancier than that should happen.



27
28
29
30
31
# File 'lib/wukong/store/cassandra_model.rb', line 27

def to_db_hash
  db_hsh = {}
  each_pair{|k,v| db_hsh[k.to_s] = v.to_s unless v.nil? }
  db_hsh
end