class MongoPersistedCollection
Collection of items that are automatically persisted to a MongoDB collection
Public Class Methods
Creates a new MongoPersistedCollection
@param [String] hostname of the MongoDB server @param [String] db the database where we want to persist the array @param [String] collection the collection we are using to persist this collection
# File lib/util/mongo_persisted_collection.rb, line 12 def initialize( hostname, db, collection ) Mongo::Logger.logger.level = ::Logger::INFO client = Mongo::Client.new([hostname], :database => db) @collection = client[collection] end
Public Instance Methods
Deletes the first element from this persisted collection that is equal to item
@param [Hash] item the object we want to delete @return [Hash] the object we just deleted
# File lib/util/mongo_persisted_collection.rb, line 33 def delete( item ) from_bson_to_hash @collection.find(item).find_one_and_delete end
Returns the length of the collection
@return [Fixnum] the length of the collection
# File lib/util/mongo_persisted_collection.rb, line 63 def length @collection.find.count.to_i end
Pushes (appends) the given hash on to the end of this persisted collection
@param [Hash] item the object we want to append @return [MongoPersistedCollection] the persisted collection itself, so several appends may be chained together
# File lib/util/mongo_persisted_collection.rb, line 23 def push( item ) @collection.insert_one item self end
Replaces the first element from this persisted array that matches item, with replacement
@param [Hash] replacement for the current element @return [Hahs] the item that was replaced, {} if the element wasn't in the collection
# File lib/util/mongo_persisted_collection.rb, line 42 def replace( item, replacement ) r = delete item push replacement r end
Returns an array representation of this collection
@return [Array<Hash>] array representation of this persisted collection
# File lib/util/mongo_persisted_collection.rb, line 52 def to_a ta = Array.new @collection.find.each do |doc| ta.push from_bson_to_hash(doc) end ta end
Private Instance Methods
# File lib/util/mongo_persisted_collection.rb, line 70 def from_bson_to_hash( item ) h = item.to_h h.delete '_id' h end