Class: Cequel::Record::List
- Inherits:
-
Array
- Object
- Array
- Cequel::Record::List
- 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.
Constant Summary collapse
- NON_ATOMIC_MUTATORS =
These methods are not available on lists because they require reading collection data before writing it.
[ :collect!, :delete_if, :fill, :flatten!, :insert, :keep_if, :map!, :pop, :reject!, :reverse!, :rotate!, :select!, :shift, :shuffle!, :slice!, :sort!, :sort_by!, :uniq! ]
Class Method Summary collapse
-
.empty ⇒ Array
private
An empty array.
Instance Method Summary collapse
-
#[]=(position, *args) ⇒ void
Set the value at a position or range of positions.
-
#clear ⇒ List
Remove all elements from the list.
-
#concat(array) ⇒ List
Concatenate another collection onto this list.
-
#delete(object) ⇒ List
Remove all instances of a given value from the list.
-
#delete_at(index) ⇒ List
Remove the element at a given position from the list.
-
#push(*objects) ⇒ List
(also: #<<, #append)
Push (append) one or more elements to the end of the list.
-
#replace(array) ⇒ List
Replace the entire contents of this list with a new collection.
-
#unshift(*objects) ⇒ List
(also: #prepend)
Prepend one or more values to the beginning of this list.
Methods included from Collection
#column_name, #initialize, #inspect, #loaded!, #loaded?, #persisted!
Class Method Details
.empty ⇒ Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns an empty array.
192 |
# File 'lib/cequel/record/collection.rb', line 192 def self.empty; []; end |
Instance Method Details
#[]=(position, element) ⇒ void #[]=(range, elements) ⇒ void #[]=(start_position, count, elements) ⇒ void
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.
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/cequel/record/collection.rb', line 223 def []=(position, *args) if position.is_a?(Range) first, count = position.first, position.count else first, count = position, args[-2] end element = args[-1] = if args[-1].is_a?(Array) then cast_collection(args[-1]) else cast_element(args[-1]) end if first < 0 fail ArgumentError, "Bad index #{position}: CQL lists do not support negative " \ "indices" end if count.nil? updater.list_replace(column_name, first, element) else element = Array.wrap(element) count.times do |i| if i < element.length updater.list_replace(column_name, first+i, element[i]) else deleter.list_remove_at(column_name, first+i) end end end to_modify { super } end |
#clear ⇒ List
Remove all elements from the list. This will propagate to the database as a DELETE of the list column.
262 263 264 265 |
# File 'lib/cequel/record/collection.rb', line 262 def clear deleter.delete_columns(column_name) to_modify { super } end |
#concat(array) ⇒ List
Concatenate another collection onto this list.
273 274 275 276 277 |
# File 'lib/cequel/record/collection.rb', line 273 def concat(array) array = cast_collection(array) updater.list_append(column_name, array) to_modify { super } end |
#delete(object) ⇒ List
Remove all instances of a given value from the list.
285 286 287 288 289 |
# File 'lib/cequel/record/collection.rb', line 285 def delete(object) object = cast_element(object) updater.list_remove(column_name, object) to_modify { super } end |
#delete_at(index) ⇒ List
Remove the element at a given position from the list.
297 298 299 300 |
# File 'lib/cequel/record/collection.rb', line 297 def delete_at(index) 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.
308 309 310 311 312 |
# File 'lib/cequel/record/collection.rb', line 308 def push(*objects) objects.map! { |object| cast_element(object) } updater.list_append(column_name, objects) to_modify { super } end |
#replace(array) ⇒ List
Replace the entire contents of this list with a new collection
322 323 324 325 326 |
# File 'lib/cequel/record/collection.rb', line 322 def replace(array) array = cast_collection(array) 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
334 335 336 337 338 |
# File 'lib/cequel/record/collection.rb', line 334 def unshift(*objects) objects.map!(&method(:cast_element)) updater.list_prepend(column_name, objects.reverse) to_modify { super } end |