Module: CassandraMapper::Persistence
- Included in:
- Base
- Defined in:
- lib/cassandra_mapper/persistence.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #_check_key ⇒ Object
- #_determine_transform_options ⇒ Object
- #create(uniq_key, options) ⇒ Object
- #delete ⇒ Object
- #destroy ⇒ Object
- #destroyed? ⇒ Boolean
- #loaded! ⇒ Object
- #save(with_validation = true) ⇒ Object
- #to_mutation(with_validation = true, options = {}) ⇒ Object
- #update(uniq_key, options) ⇒ Object
- #write!(uniq_key, options) ⇒ Object
Class Method Details
.included(klass) ⇒ Object
306 307 308 |
# File 'lib/cassandra_mapper/persistence.rb', line 306 def self.included(klass) klass.extend ClassMethods end |
Instance Method Details
#_check_key ⇒ Object
91 92 93 94 95 |
# File 'lib/cassandra_mapper/persistence.rb', line 91 def _check_key uniq_key = self.key raise CassandraMapper::UndefinedKeyException if uniq_key.nil? uniq_key end |
#_determine_transform_options ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/cassandra_mapper/persistence.rb', line 3 def = {:string_keys => true} is_update = false if new_record? [:defined] = true else return false unless changed_attributes.length > 0 [:changed] = true is_update = true end [, is_update] end |
#create(uniq_key, options) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/cassandra_mapper/persistence.rb', line 35 def create(uniq_key, ) _run_create_callbacks do write!(uniq_key, ) self.new_record = false self end end |
#delete ⇒ Object
97 98 99 100 101 102 |
# File 'lib/cassandra_mapper/persistence.rb', line 97 def delete self.class.delete(_check_key) unless new_record? @destroyed = true freeze self end |
#destroy ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/cassandra_mapper/persistence.rb', line 50 def destroy unless new_record? _run_destroy_callbacks do self.class.delete(_check_key) end end @destroyed = true freeze self end |
#destroyed? ⇒ Boolean
104 105 106 |
# File 'lib/cassandra_mapper/persistence.rb', line 104 def destroyed? (@destroyed && true) || false end |
#loaded! ⇒ Object
29 30 31 32 33 |
# File 'lib/cassandra_mapper/persistence.rb', line 29 def loaded! self.new_record = false _run_load_callbacks self end |
#save(with_validation = true) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/cassandra_mapper/persistence.rb', line 16 def save(with_validation = true) _run_save_callbacks do uniq_key = _check_key , is_update = return false unless if is_update update(uniq_key, ) else create(uniq_key, ) end end end |
#to_mutation(with_validation = true, options = {}) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/cassandra_mapper/persistence.rb', line 78 def to_mutation(with_validation = true, = {}) uniq_key = _check_key.to_s = .delete(:timestamp) || Time.stamp general_opts, is_update = return false unless general_opts .merge!(general_opts) { uniq_key => { self.class.column_family.to_s => self.class.to_mutation(to_simple(), ) } } end |
#update(uniq_key, options) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/cassandra_mapper/persistence.rb', line 43 def update(uniq_key, ) _run_update_callbacks do write!(uniq_key, .merge({ :with_delete => true })) self end end |
#write!(uniq_key, options) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/cassandra_mapper/persistence.rb', line 61 def write!(uniq_key, ) with_delete = .delete(:with_delete) structure = to_simple() if with_delete deletes = self.class.prune_deletes_from_simple_structure(structure) cassandra_args = [] if ! (structure.empty? or deletes.empty?) or deletes.size >= 2 cassandra_args << {:timestamp => Time.stamp} end base_args = [self.class.column_family, uniq_key] connection.insert(*(base_args + [structure] + cassandra_args)) unless structure.empty? deletes.each {|del_args| connection.remove(*(base_args + del_args + cassandra_args))} else connection.insert(self.class.column_family, uniq_key, to_simple()) end end |