Module: CassandraObject::Persistence::ClassMethods
- Defined in:
- lib/cassandra_object/persistence.rb
Instance Method Summary collapse
- #_key ⇒ Object
- #batch(&block) ⇒ Object
- #batching? ⇒ Boolean
- #create(attributes = {}, &block) ⇒ Object
- #delete(ids, attributes = []) ⇒ Object
- #delete_all ⇒ Object
- #delete_schema(obj) ⇒ Object
- #encode_attributes(attributes) ⇒ Object
- #insert_record(id, attributes) ⇒ Object
- #instantiate(id, attributes) ⇒ Object
- #remove(ids) ⇒ Object
- #ttl ⇒ Object
- #ttl=(value) ⇒ Object
- #update(id, attributes) ⇒ Object
- #update_record(id, attributes) ⇒ Object
Instance Method Details
#_key ⇒ Object
23 24 25 26 |
# File 'lib/cassandra_object/persistence.rb', line 23 def _key # todo only mono primary id for now self.keys.tr('()','').split(',').first end |
#batch(&block) ⇒ Object
90 91 92 |
# File 'lib/cassandra_object/persistence.rb', line 90 def batch(&block) adapter.batch(&block) end |
#batching? ⇒ Boolean
86 87 88 |
# File 'lib/cassandra_object/persistence.rb', line 86 def batching? adapter.batching? end |
#create(attributes = {}, &block) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/cassandra_object/persistence.rb', line 32 def create(attributes = {}, &block) self.ttl = attributes.delete(:ttl) if self.schema_type != :dynamic_attributes new(attributes, &block).tap do |object| object.save end else key = attributes[:key] insert_record key.to_s, attributes.except(:key).stringify_keys attributes end end |
#delete(ids, attributes = []) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/cassandra_object/persistence.rb', line 49 def delete(ids, attributes = []) ids = [ids] if !ids.is_a?(Array) if self.schema_type == :standard attrs = attributes.is_a?(Array) ? {} : attributes adapter.delete self, ids, attrs elsif attributes.blank? adapter.delete column_family, ids else attr = {} attributes.each{|a| attr[a] = nil} ids.each do |id| adapter.update column_family, id, encode_attributes(attr) end end end |
#delete_all ⇒ Object
28 29 30 |
# File 'lib/cassandra_object/persistence.rb', line 28 def delete_all adapter.execute "TRUNCATE #{column_family}" end |
#delete_schema(obj) ⇒ Object
66 67 68 |
# File 'lib/cassandra_object/persistence.rb', line 66 def delete_schema(obj) adapter.delete_single(obj) end |
#encode_attributes(attributes) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/cassandra_object/persistence.rb', line 103 def encode_attributes(attributes) encoded = {} attributes.each do |column_name, value| if value.nil? encoded[column_name] = nil else if self.schema_type == :dynamic_attributes encoded[column_name] = value.to_s elsif self.schema_type == :standard encoded[column_name] = value else encoded[column_name] = attribute_definitions[column_name].coder.encode(value) end end end encoded end |
#insert_record(id, attributes) ⇒ Object
70 71 72 73 74 |
# File 'lib/cassandra_object/persistence.rb', line 70 def insert_record(id, attributes) attributes = attributes.dup attributes[self._key] = id if self.schema_type == :standard adapter.insert column_family, id, encode_attributes(attributes), self.ttl end |
#instantiate(id, attributes) ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/cassandra_object/persistence.rb', line 94 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('@model_attributes', typecast_persisted_attributes(object, attributes)) end end |
#remove(ids) ⇒ Object
19 20 21 |
# File 'lib/cassandra_object/persistence.rb', line 19 def remove(ids) delete ids end |
#ttl ⇒ Object
15 16 17 |
# File 'lib/cassandra_object/persistence.rb', line 15 def ttl @ttl ||= nil end |
#ttl=(value) ⇒ Object
11 12 13 |
# File 'lib/cassandra_object/persistence.rb', line 11 def ttl=(value) @ttl = value end |
#update(id, attributes) ⇒ Object
45 46 47 |
# File 'lib/cassandra_object/persistence.rb', line 45 def update(id, attributes) update_record(id, attributes) end |
#update_record(id, attributes) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/cassandra_object/persistence.rb', line 76 def update_record(id, attributes) return if attributes.empty? if self.schema_type == :standard attributes = attributes.dup attributes[self._key] = id id = self._key end adapter.update column_family, id, encode_attributes(attributes), self.ttl end |