class MongoPersistedCollection

Collection of items that are automatically persisted to a MongoDB collection

Public Class Methods

new( hostname, db, collection ) click to toggle source

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

delete( item ) click to toggle source

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
length() click to toggle source

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
push( item ) click to toggle source

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
replace( item, replacement ) click to toggle source

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
to_a() click to toggle source

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

from_bson_to_hash( item ) click to toggle source
# File lib/util/mongo_persisted_collection.rb, line 70
def from_bson_to_hash( item )
  h = item.to_h
  h.delete '_id'
  h
end