Class: Locomotive::Steam::Adapters::MongoDB::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/locomotive/steam/adapters/mongodb/command.rb

Instance Method Summary collapse

Constructor Details

#initialize(collection, mapper) ⇒ Command

Returns a new instance of Command.



7
8
9
10
# File 'lib/locomotive/steam/adapters/mongodb/command.rb', line 7

def initialize(collection, mapper)
  @collection = collection
  @mapper     = mapper
end

Instance Method Details

#delete(entity) ⇒ Object



38
39
40
# 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) ⇒ Object



30
31
32
33
34
35
36
# 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) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# 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) ⇒ Object



23
24
25
26
27
28
# 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