Module: Ecoportal::API::Common::Content::CollectionModel::Mutation::Upsert
- Extended by:
- Includer
- Defined in:
- lib/ecoportal/api/common/content/collection_model/mutation/upsert.rb
Instance Method Summary collapse
-
#upsert!(value, pos: NOT_USED, before: NOT_USED, after: NOT_USED) ⇒ Object
Tries to find the element
value, if it exists, it updates it Otherwise it pushes it to the end.
Methods included from Includer
Instance Method Details
#upsert!(value, pos: NOT_USED, before: NOT_USED, after: NOT_USED) ⇒ Object
Tries to find the element value, if it exists, it updates it
Otherwise it pushes it to the end
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/ecoportal/api/common/content/collection_model/mutation/upsert.rb', line 28 def upsert!(value, pos: NOT_USED, before: NOT_USED, after: NOT_USED) unless value.is_a?(Hash) || value.is_a?(Content::DoubleModel) msg = "'Content::DoubleModel' or 'Hash' doc required. Given #{value.class}" raise ArgumentError, msg 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).tap do |pos_idx| _items.insert(pos_idx, new_item(item_doc)) @indexed = false end end (item || self[item_doc]).tap do |itm| yield(itm) if block_given? end end |