lib/ecoportal/api/common/content/collection_model.rb in ecoportal-api-v2-0.8.8 vs lib/ecoportal/api/common/content/collection_model.rb in ecoportal-api-v2-0.8.9
- old
+ new
@@ -1,9 +1,12 @@
module Ecoportal
module API
module Common
module Content
+ # CollectionModel aims to deal with Arrays of actual objects.
+ # @note to be able to refer to the correct element of the Collection,
+ # it is required that those elements have a unique `key` that allows to identify them
class CollectionModel < Content::DoubleModel
class << self
attr_writer :klass
attr_accessor :order_matters, :order_key
@@ -165,10 +168,11 @@
(item || self[item_doc]).tap do |item|
yield(item) if block_given?
end
end
+ # Deletes `value` from this `CollectionModel` instance
def delete!(value)
unless value.is_a?(Hash) || value.is_a?(Content::DoubleModel)
raise "'Content::DoubleModel' or 'Hash' doc required"
end
if item = self[value]
@@ -184,11 +188,11 @@
def on_change
variables_remove!
end
- # Gets the `key` of the object
+ # Gets the `key` of the object `value`
def get_key(value)
case value
when Content::DoubleModel
value.key
when Hash
@@ -233,14 +237,17 @@
def variables_remove!
@indexed = false
super
end
+ # Deletes `value` from `doc` (here referred as `_doc_items`)
+ # @return [Object] the element deleted from `doc`
def _doc_delete(value)
if current_pos = _doc_key(value)
- _doc_items.delete_at(current_pos)
- on_change
+ _doc_items.delete_at(current_pos).tap do |deleted|
+ on_change
+ end
end
end
def _doc_upsert(value, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
current_pos = _doc_key(value)
@@ -248,10 +255,9 @@
when used_param?(pos)
pos
when used_param?(before)
_doc_key(before)
when used_param?(after)
- #puts "to add after #{after.id}"
if i = _doc_key(after)
i + 1
end
end