Class: Cequel::Record::List

Inherits:
Array
  • Object
show all
Includes:
Collection
Defined in:
lib/cequel/record/collection.rb

Overview

The value of a list column in a Cequel::Record instance. List collections encapsulate and behave like the built-in ‘Array` type.

See Also:

Since:

  • 1.0.0

Constant Summary collapse

NON_ATOMIC_MUTATORS =

These methods are not available on lists because they require reading collection data before writing it.

Since:

  • 1.0.0

[
  :collect!,
  :delete_if,
  :fill,
  :flatten!,
  :insert,
  :keep_if,
  :map!,
  :pop,
  :reject!,
  :reverse!,
  :rotate!,
  :select!,
  :shift,
  :shuffle!,
  :slice!,
  :sort!,
  :sort_by!,
  :uniq!
]

Instance Method Summary collapse

Methods included from Collection

#column_name, #initialize, #inspect, #loaded!, #loaded?, #persisted!

Methods included from Util::Forwardable

#delegate

Instance Method Details

#[]=(position, element) ⇒ void #[]=(range, elements) ⇒ void #[]=(start_position, count, elements) ⇒ void

Note:

Negative positions are not supported, as they are not allowed in CQL list operations.

This method returns an undefined value.

Set the value at a position or range of positions. This modification will be staged and persisted as an atomic list update when the record is saved. If the collection data is loaded in memory, it will also be modified accordingly.

Overloads:

  • #[]=(position, element) ⇒ void

    Parameters:

    • position (Integer)

      position at which to set element

    • element

      element to insert at position in list

  • #[]=(range, elements) ⇒ void

    Parameters:

    • range (Range)

      range of positions at which to replace elements

    • elements (Array)

      new elements to replace in this range

  • #[]=(start_position, count, elements) ⇒ void

    Parameters:

    • start_position (Integer)

      position at which to begin replacing elements

    • count (Integer)

      number of elements to replace

    • elements (Array)

      new elements to replace in this range

See Also:

  • DataSet#list_replace

Since:

  • 1.0.0



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/cequel/record/collection.rb', line 220

def []=(*args)
  if args[0].is_a?(Fixnum) && args.count == 2
    # single element set/replace
    elem = cast_element(args[1])

    to_update do
      set_item(args[0], elem)
    end
    to_modify { super(args[0], elem) }

  else
    # multi-element set/replace
    range = if args[0].is_a?(Range)
              args[0]
            elsif args[0].is_a?(Fixnum) && args[1].is_a?(Fixnum)
              args[0]..(args[0]+args[1]-1)
            else
              Kernel.raise ArgumentError, "[i]=elem or [i,count]=elems or [a..b]=elems"
            end
    elems = cast_collection(Array.wrap(args[-1]))

    to_update do
      set_range(range, elems, args[-1])
    end
    to_modify { super(range, elems) }
  end
end

#clearList

Remove all elements from the list. This will propagate to the database as a DELETE of the list column.

Returns:

Since:

  • 1.0.0



254
255
256
257
# File 'lib/cequel/record/collection.rb', line 254

def clear
  to_update { deleter.delete_columns(column_name) }
  to_modify { super }
end

#concat(array) ⇒ List

Concatenate another collection onto this list.

Parameters:

  • array (Array)

    elements to concatenate

Returns:

Since:

  • 1.0.0



265
266
267
268
269
# File 'lib/cequel/record/collection.rb', line 265

def concat(array)
  array = cast_collection(array)
  to_update { updater.list_append(column_name, array) }
  to_modify { super }
end

#delete(object) ⇒ List

Remove all instances of a given value from the list.

Parameters:

  • object

    value to remove

Returns:

Since:

  • 1.0.0



277
278
279
280
281
# File 'lib/cequel/record/collection.rb', line 277

def delete(object)
  object = cast_element(object)
  to_update { updater.list_remove(column_name, object) }
  to_modify { super }
end

#delete_at(index) ⇒ List

Remove the element at a given position from the list.

Parameters:

  • index (Integer)

    position from which to remove the element

Returns:

Since:

  • 1.0.0



289
290
291
292
# File 'lib/cequel/record/collection.rb', line 289

def delete_at(index)
  to_update { deleter.list_remove_at(column_name, index) }
  to_modify { super }
end

#push(*objects) ⇒ List Also known as: <<, append

Push (append) one or more elements to the end of the list.

Parameters:

  • objects

    value(s) to add to the end of the list

Returns:

Since:

  • 1.0.0



300
301
302
303
304
# File 'lib/cequel/record/collection.rb', line 300

def push(*objects)
  objects.map! { |object| cast_element(object) }
  to_update { updater.list_append(column_name, objects) }
  to_modify { super }
end

#replace(array) ⇒ List

Replace the entire contents of this list with a new collection

Parameters:

  • array (Array)

    new elements for this list

Returns:

Since:

  • 1.0.0



314
315
316
317
318
# File 'lib/cequel/record/collection.rb', line 314

def replace(array)
  array = cast_collection(array)
  to_update { updater.set(column_name => array) }
  to_modify { super }
end

#unshift(*objects) ⇒ List Also known as: prepend

Prepend one or more values to the beginning of this list

Parameters:

  • objects

    value(s) to add to the beginning of the list

Returns:

Since:

  • 1.0.0



326
327
328
329
330
331
# File 'lib/cequel/record/collection.rb', line 326

def unshift(*objects)
  objects.map!(&method(:cast_element))
  prepared = @model.class.connection.bug8733_version? ? objects.reverse : objects
  to_update { updater.list_prepend(column_name, prepared) }
  to_modify { super }
end