class Locomotive::Steam::Adapters::MongoDB::Command

Public Class Methods

new(collection, mapper) click to toggle source
# File lib/locomotive/steam/adapters/mongodb/command.rb, line 7
def initialize(collection, mapper)
  @collection = collection
  @mapper     = mapper
end

Public Instance Methods

delete(entity) click to toggle source
# File lib/locomotive/steam/adapters/mongodb/command.rb, line 38
def delete(entity)
  @collection.find(_id: entity._id).delete_one
end
inc(entity, attribute, amount = 1) click to toggle source
# File lib/locomotive/steam/adapters/mongodb/command.rb, line 30
def inc(entity, attribute, amount = 1)
  entity.tap do
    @collection.find(_id: entity._id).update_one('$inc' => { attribute => amount })
    entity[attribute] ||= 0
    entity[attribute] += amount
  end
end
insert(entity) click to toggle source
# File lib/locomotive/steam/adapters/mongodb/command.rb, line 12
def insert(entity)
  # make sure the entity gets a valid id
  entity[:_id] ||= BSON::ObjectId.new

  serialized_entity = @mapper.serialize(entity)

  @collection.insert_one(serialized_entity)

  entity
end
update(entity) click to toggle source
# File lib/locomotive/steam/adapters/mongodb/command.rb, line 23
def update(entity)
  entity.tap do
    serialized_entity = @mapper.serialize(entity)
    @collection.find(_id: entity._id).update_one(serialized_entity)
  end
end