Module: CassandraMapper::Persistence::ClassMethods

Defined in:
lib/cassandra_mapper/persistence.rb

Defined Under Namespace

Classes: SimpleMutator, SuperMutator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



204
205
206
207
208
209
210
# File 'lib/cassandra_mapper/persistence.rb', line 204

def self.extended(klass)
  klass.module_eval do
    extend CassandraMapper::Support::Callbacks
    define_model_callbacks :save, :create, :update, :destroy
    define_model_callbacks :load, :only => :after
  end
end

Instance Method Details

#column_family(family = nil) ⇒ Object



199
200
201
202
# File 'lib/cassandra_mapper/persistence.rb', line 199

def column_family(family = nil)
  @cassandra_mapper_column_family = family if ! family.nil?
  @cassandra_mapper_column_family
end

#connectionObject



191
192
193
# File 'lib/cassandra_mapper/persistence.rb', line 191

def connection
  @cassandra_mapper_connection
end

#connection=(connection) ⇒ Object



195
196
197
# File 'lib/cassandra_mapper/persistence.rb', line 195

def connection=(connection)
  @cassandra_mapper_connection = connection
end

#delete(key) ⇒ Object

Given a key of a single row key or array of row keys, removes the rows from the Cassandra column family associated with the receiving class.

As the underlying Cassandra client returns no information about the result of the operation, the result is always nil.

The delete operation is purely database-side; no objects are instantiated and therefore no object callbacks take place for the deleted rows. This makes delete risky for classes that manage associations, indexes, etc.



164
165
166
167
168
169
170
# File 'lib/cassandra_mapper/persistence.rb', line 164

def delete(key)
  keys = Array === key ? key : [key]
  keys.each do |key|
    connection.remove(column_family, key)
  end
  nil
end

#destroy(key) ⇒ Object

Similar to delete; given a key of a single row key or array of row keys, removes the rows from the Cassandra column family associated with the receiving class.

However, unlike delete, destroy loads up the object per row key in turn and invokes destroy on each object. This allows for callbacks and observers to be executed, which is essential for index maintenance, association maintenance, etc.



180
181
182
183
184
185
186
187
188
189
# File 'lib/cassandra_mapper/persistence.rb', line 180

def destroy(key)
  objs = find(key)
  case objs
    when Array
      objs.each {|obj| obj.destroy}
    else
      objs.destroy
  end
  nil
end

#find(*args) ⇒ Object

Given a single key or list of keys, returns all mapped objects found for thoese keys.

If the row for a specified key is missing, a CassndraMapper::RecordNotFoundException exception is raised. This can be overridden by specifying the :allow_missing option (:allow_missing => true)

Keys and options may be specified in a variety of ways:

  • Flat list

    SomeClass.find(key1, key2, key3, options)
    
  • Separate lists

    SomeClass.find([key1, key2, key3], options)
    

And of course, options can always be left out.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/cassandra_mapper/persistence.rb', line 123

def find(*args)
  single = false
  case args.first
    when Array
      keys = args.first
    when nil
      raise CassandraMapper::InvalidArgumentException
    else
      keys = args
      single = true if keys.length == 1
  end
  case args.last
    when Hash
      options = args.pop
    else
      options = {}
  end

  result = connection.multi_get(column_family, keys).values.inject([]) do |arr, hash|
    if not hash.empty?
      obj = new(hash)
      obj.new_record = false
      arr << obj
      obj.loaded!
    end
    arr
  end
  raise CassandraMapper::RecordNotFoundException unless result.size == keys.size or options[:allow_missing]
  single ? result.first : result
end

#mutatorObject



216
217
218
219
220
221
222
# File 'lib/cassandra_mapper/persistence.rb', line 216

def mutator
  unless @mutator
    mutator_class = simple_mapper.attributes.first[1].mapper ? SuperMutator : SimpleMutator
    @mutator = mutator_class.new
  end
  @mutator
end

#prune_deletes(source, deletes, context) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/cassandra_mapper/persistence.rb', line 285

def prune_deletes(source, deletes, context)
  source.each do |k, v|
    if v.nil?
      deletes << context + [k]
      source.delete(k)
    # pre 1.9 String responds to :each; but :values_at is a
    # safe bet for 1.8 and 1.9 for array, hash, but not string.
    elsif v.respond_to?(:values_at)
      prune_deletes(v, deletes, context + [k])
      source.delete(k) if v.empty?
    end
  end
end

#prune_deletes_from_simple_structure(structure) ⇒ Object



299
300
301
302
303
# File 'lib/cassandra_mapper/persistence.rb', line 299

def prune_deletes_from_simple_structure(structure)
  deletes = []
  prune_deletes(structure, deletes, [])
  deletes
end

#to_mutation(simple_structure, timestamp) ⇒ Object



212
213
214
# File 'lib/cassandra_mapper/persistence.rb', line 212

def to_mutation(simple_structure, timestamp)
  mutator.from_simple(simple_structure, timestamp)
end