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

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_keyObject



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_optionsObject



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/cassandra_mapper/persistence.rb', line 3

def _determine_transform_options
  options = {:string_keys => true}
  is_update = false
  if new_record?
    options[:defined] = true
  else
    return false unless changed_attributes.length > 0
    options[:changed] = true
    is_update = true
  end
  [options, 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, options)
  _run_create_callbacks do
    write!(uniq_key, options)
    self.new_record = false
    self
  end
end

#deleteObject



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

#destroyObject



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

Returns:

  • (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
    options, is_update = _determine_transform_options
    return false unless options
    if is_update
      update(uniq_key, options)
    else
      create(uniq_key, options)
    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, options = {})
  uniq_key = _check_key.to_s
  timestamp = options.delete(:timestamp) || Time.stamp
  general_opts, is_update = _determine_transform_options
  return false unless general_opts
  options.merge!(general_opts)
  {
    uniq_key => {
      self.class.column_family.to_s => self.class.to_mutation(to_simple(options), timestamp)
    }
  }
end

#update(uniq_key, options) ⇒ Object



43
44
45
46
47
48
# File 'lib/cassandra_mapper/persistence.rb', line 43

def update(uniq_key, options)
  _run_update_callbacks do
    write!(uniq_key, options.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, options)
  with_delete = options.delete(:with_delete)
  structure = to_simple(options)
  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(options))
  end
end