Sha256: ecb9b3a936bb9f9157067f75536f6d1fae4c5cc3dd8c7903605433cd1da8243c

Contents?: true

Size: 1.05 KB

Versions: 27

Compression:

Stored size: 1.05 KB

Contents

module Locomotive::Steam
  module Adapters
    module MongoDB

      class Command

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

        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

        def update(entity)
          entity.tap do
            serialized_entity = @mapper.serialize(entity)
            @collection.find(_id: entity._id).update_one(serialized_entity)
          end
        end

        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

        def delete(entity)
          @collection.find(_id: entity._id).delete_one
        end

      end

    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
locomotivecms_steam-1.6.1 lib/locomotive/steam/adapters/mongodb/command.rb
locomotivecms_steam-1.6.0 lib/locomotive/steam/adapters/mongodb/command.rb
locomotivecms_steam-1.6.0.rc1 lib/locomotive/steam/adapters/mongodb/command.rb
locomotivecms_steam-1.6.0.beta1 lib/locomotive/steam/adapters/mongodb/command.rb
locomotivecms_steam-1.5.3 lib/locomotive/steam/adapters/mongodb/command.rb
locomotivecms_steam-1.5.2 lib/locomotive/steam/adapters/mongodb/command.rb
locomotivecms_steam-1.5.1 lib/locomotive/steam/adapters/mongodb/command.rb
locomotivecms_steam-1.5.0 lib/locomotive/steam/adapters/mongodb/command.rb
locomotivecms_steam-1.5.0.rc1 lib/locomotive/steam/adapters/mongodb/command.rb
locomotivecms_steam-1.5.0.rc0 lib/locomotive/steam/adapters/mongodb/command.rb
locomotivecms_steam-1.5.0.beta3 lib/locomotive/steam/adapters/mongodb/command.rb
locomotivecms_steam-1.5.0.beta2 lib/locomotive/steam/adapters/mongodb/command.rb
locomotivecms_steam-1.5.0.beta1 lib/locomotive/steam/adapters/mongodb/command.rb
locomotivecms_steam-1.4.1 lib/locomotive/steam/adapters/mongodb/command.rb
locomotivecms_steam-1.4.0 lib/locomotive/steam/adapters/mongodb/command.rb
locomotivecms_steam-1.4.0.rc2 lib/locomotive/steam/adapters/mongodb/command.rb
locomotivecms_steam-1.4.0.rc1 lib/locomotive/steam/adapters/mongodb/command.rb
locomotivecms_steam-1.4.0.pre.rc.1 lib/locomotive/steam/adapters/mongodb/command.rb
locomotivecms_steam-1.3.0 lib/locomotive/steam/adapters/mongodb/command.rb
locomotivecms_steam-1.3.0.rc2 lib/locomotive/steam/adapters/mongodb/command.rb