lib/ecoportal/api/common/content/collection_model.rb in ecoportal-api-v2-0.8.9 vs lib/ecoportal/api/common/content/collection_model.rb in ecoportal-api-v2-0.8.10
- old
+ new
@@ -141,40 +141,48 @@
end
end
end
# Get an element usign the `key`.
+ # @param value [String, Hash, Ecoportal::API::Common::Content::DoubleModel]
+ # @return [Object] the `items_class` element object
def [](value)
items_by_key[get_key(value)]
end
+ # @return [Array<Object>] the `items_class` element object
def values_at(*keys)
keys.map {|key| self[key]}
end
# Tries to find the element `value`, if it exists, it updates it
- # Otherwise it pushes it to the end
- # @return the element
+ # Otherwise it pushes it to the end
+ # @value [Hash, Ecoportal::API::Common::Content::DoubleModel] the eleement to be added
+ # @return [Object] the `items_class` element object
def upsert!(value, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
unless value.is_a?(Hash) || value.is_a?(Content::DoubleModel)
raise "'Content::DoubleModel' or 'Hash' doc required"
end
item_doc = value.is_a?(Content::DoubleModel)? value.doc : value
item_doc = JSON.parse(item_doc.to_json)
if item = self[value]
item.replace_doc(item_doc)
else
- _doc_upsert(item_doc, pos: pos, before: before, after: after)
+ pos_idx = _doc_upsert(item_doc, pos: pos, before: before, after: after)
end
(item || self[item_doc]).tap do |item|
yield(item) if block_given?
end
end
# Deletes `value` from this `CollectionModel` instance
+ # @param value [String, Hash, Ecoportal::API::Common::Content::DoubleModel]
+ # - When used as `String`, the `key` value (i.e. `id` value) is expected
+ # - When used as `Hash`, it should be the `doc` of the target element
+ # - When used as `DoubleModel`, it should be the specific object to be deleted
def delete!(value)
- unless value.is_a?(Hash) || value.is_a?(Content::DoubleModel)
+ unless value.is_a?(Hash) || value.is_a?(Content::DoubleModel) || value.is_a?(String)
raise "'Content::DoubleModel' or 'Hash' doc required"
end
if item = self[value]
_doc_delete(item.doc)
end
@@ -197,10 +205,12 @@
value.key
when Hash
value[items_key]
when String
value
+ when Numeric
+ get_key(self.to_a[value])
end
end
def _doc_items
replace_doc([]) unless doc.is_a?(Array)
@@ -248,35 +258,44 @@
end
end
end
def _doc_upsert(value, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
- current_pos = _doc_key(value)
- pos = case
- when used_param?(pos)
- pos
- when used_param?(before)
- _doc_key(before)
- when used_param?(after)
- if i = _doc_key(after)
- i + 1
- end
- end
+ current_pos = if elem = self[value]
+ _doc_key(elem)
+ end
+ pos = scope_position(pos: pos, before: before, after: after)
pos ||= current_pos
if current_pos && pos
_doc_items.delete_at(current_pos)
pos = (pos <= current_pos)? pos : pos - 1
end
pos = (pos && pos < _doc_items.length)? pos : _doc_items.length
-
pos.tap do |i|
_doc_items.insert(pos, value)
on_change
end
+ end
+
+ def scope_position(pos: NOT_USED, before: NOT_USED, after: NOT_USED)
+ case
+ when used_param?(pos)
+ if elem = self[pos]
+ _doc_key(elem) - 1
+ end
+ when used_param?(before)
+ if elem = self[before]
+ _doc_key(elem) - 1
+ end
+ when used_param?(after)
+ if elem = self[after]
+ _doc_key(elem)
+ end
+ end
end
end
end
end