Class: Candy::CandyArray
- Inherits:
-
Object
- Object
- Candy::CandyArray
- Includes:
- Crunch, Embeddable
- Defined in:
- lib/candy/array.rb
Overview
An array-like object that saves itself to a parent Candy::Piece object. MongoDB’s atomic array operators are used extensively to perform concurrency-friendly updates of individual array elements without rewriting the whole array.
Class Method Summary collapse
-
.embed(parent, attribute, *args) ⇒ Object
Creates the object with parent and attribute values set properly on the object and any children.
Instance Method Summary collapse
-
#<<(val) ⇒ Object
(also: #push)
Appends a value to our array.
-
#==(val) ⇒ Object
Array equality.
-
#[](index) ⇒ Object
Retrieves the value from our internal array.
-
#[]=(index, val) ⇒ Object
Set a value at a specified index in our array.
-
#candy ⇒ Object
(also: #to_candy, #to_ary)
Returns the array of memoized values.
-
#from_candy(array) ⇒ Object
Unwraps all elements of the array, telling them who their parent is.
-
#initialize(*args) ⇒ CandyArray
constructor
Sets the initial array state.
-
#length ⇒ Object
(also: #size)
Array length.
-
#shift(n = 1) ⇒ Object
Pops the front off the MongoDB array and returns it, then resyncs the array.
Methods included from Embeddable
Methods included from Crunch
Constructor Details
#initialize(*args) ⇒ CandyArray
Sets the initial array state.
20 21 22 23 |
# File 'lib/candy/array.rb', line 20 def initialize(*args) @__candy = from_candy(args) super() end |
Class Method Details
.embed(parent, attribute, *args) ⇒ Object
Creates the object with parent and attribute values set properly on the object and any children.
14 15 16 17 |
# File 'lib/candy/array.rb', line 14 def self.(parent, attribute, *args) this = self.new(*args) this.candy_adopt(parent, attribute) end |
Instance Method Details
#<<(val) ⇒ Object Also known as: push
Appends a value to our array.
40 41 42 43 44 |
# File 'lib/candy/array.rb', line 40 def <<(val) property = candy_coat(@__candy_parent_key, val) @__candy_parent.operate :push, @__candy_parent_key => property self.candy << property end |
#==(val) ⇒ Object
Array equality.
68 69 70 |
# File 'lib/candy/array.rb', line 68 def ==(val) self.to_ary == val end |
#[](index) ⇒ Object
Retrieves the value from our internal array.
35 36 37 |
# File 'lib/candy/array.rb', line 35 def [](index) candy[index] end |
#[]=(index, val) ⇒ Object
Set a value at a specified index in our array. Note that this operation _does not_ confirm that the array in Mongo still matches our current state. If concurrent updates have happened, you might end up overwriting something other than what you thought.
28 29 30 31 32 |
# File 'lib/candy/array.rb', line 28 def []=(index, val) property = candy_coat(nil, val) # There are no attribute names on array inheritance @__candy_parent.set (index => property) self.candy[index] = property end |
#candy ⇒ Object Also known as: to_candy, to_ary
Returns the array of memoized values.
56 57 58 |
# File 'lib/candy/array.rb', line 56 def candy @__candy ||= [] end |
#from_candy(array) ⇒ Object
Unwraps all elements of the array, telling them who their parent is. The attribute doesn’t matter because arrays don’t have them.
63 64 65 |
# File 'lib/candy/array.rb', line 63 def from_candy(array) array.map {|element| Wrapper.unwrap(element, self)} end |
#length ⇒ Object Also known as: size
Array length.
73 74 75 |
# File 'lib/candy/array.rb', line 73 def length self.to_ary.length end |
#shift(n = 1) ⇒ Object
Pops the front off the MongoDB array and returns it, then resyncs the array. (Thus supporting real-time concurrency for queue-like behavior.)
49 50 51 52 53 |
# File 'lib/candy/array.rb', line 49 def shift(n=1) doc = @__candy_parent.collection.find_and_modify query: {"_id" => @__candy_parent.id}, update: {'$pop' => {@__candy_parent_key => -1}}, new: false @__candy = doc[@__candy_parent_key.to_s] @__candy.shift end |