Module: CassandraObject::Persistence::ClassMethods
- Defined in:
- lib/cassandra_object/persistence.rb
Instance Method Summary collapse
- #batch ⇒ Object
- #batching? ⇒ Boolean
- #create(attributes = {}, &block) ⇒ Object
- #delete_all ⇒ Object
- #encode_attributes(attributes) ⇒ Object
- #instantiate(id, attributes) ⇒ Object
- #remove(id) ⇒ Object
- #write(id, attributes) ⇒ Object
Instance Method Details
#batch ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/cassandra_object/persistence.rb', line 40 def batch self.batch_statements = [] yield execute_cql(batch_statement) if batch_statements.any? ensure self.batch_statements = nil end |
#batching? ⇒ Boolean
36 37 38 |
# File 'lib/cassandra_object/persistence.rb', line 36 def batching? !batch_statements.nil? end |
#create(attributes = {}, &block) ⇒ Object
18 19 20 21 22 |
# File 'lib/cassandra_object/persistence.rb', line 18 def create(attributes = {}, &block) new(attributes, &block).tap do |object| object.save end end |
#delete_all ⇒ Object
14 15 16 |
# File 'lib/cassandra_object/persistence.rb', line 14 def delete_all execute_cql "TRUNCATE #{column_family}" end |
#encode_attributes(attributes) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/cassandra_object/persistence.rb', line 57 def encode_attributes(attributes) encoded = {} attributes.each do |column_name, value| unless value.nil? encoded[column_name.to_s] = attribute_definitions[column_name.to_sym].coder.encode(value) end end encoded end |
#instantiate(id, attributes) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/cassandra_object/persistence.rb', line 48 def instantiate(id, attributes) allocate.tap do |object| object.instance_variable_set("@id", id) if id object.instance_variable_set("@new_record", false) object.instance_variable_set("@destroyed", false) object.instance_variable_set("@attributes", typecast_attributes(object, attributes)) end end |
#remove(id) ⇒ Object
10 11 12 |
# File 'lib/cassandra_object/persistence.rb', line 10 def remove(id) execute_batchable_cql "DELETE FROM #{column_family}#{write_option_string} WHERE KEY = ?", id end |
#write(id, attributes) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/cassandra_object/persistence.rb', line 24 def write(id, attributes) if (encoded = encode_attributes(attributes)).any? insert_attributes = {'KEY' => id}.update encode_attributes(attributes) statement = "INSERT INTO #{column_family} (#{quote_columns(insert_attributes.keys) * ','}) VALUES (#{Array.new(insert_attributes.size, '?') * ','})#{write_option_string}" execute_batchable_cql statement, *insert_attributes.values end if (nil_attributes = attributes.select { |key, value| value.nil? }).any? execute_batchable_cql "DELETE #{quote_columns(nil_attributes.keys) * ','} FROM #{column_family}#{write_option_string} WHERE KEY = ?", id end end |