Sha256: c42ead3704651c136de651f6e5d9037ea12ab5dd624cc3bc0b446c5c94f74708

Contents?: true

Size: 2 KB

Versions: 20

Compression:

Stored size: 2 KB

Contents

# Copyright (C) 2014-2019 MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module Mongo
  module ChangeStreams
    class Operation

      # The operation name.
      #
      # @return [ String ] name The operation name.
      #
      # @since 2.6.0
      attr_reader :name

      # Instantiate the operation.
      #
      # @return [ Hash ] spec The operation spec.
      #
      # @since 2.6.0
      def initialize(spec)
        @spec = spec
        @name = spec['name']
      end

      def execute(db1, db2)
        db = case @spec['database']
             when db1.name
               db1
             when db2.name
               db2
             end

        send(Utils.underscore(@spec['name']) ,db[@spec['collection']])
      end

      private

      def insert_one(coll)
        coll.insert_one(document)
      end

      def update_one(coll)
        coll.update_one(filter, arguments['update'])
      end

      def replace_one(coll)
        coll.replace_one(filter, arguments['replacement'])
      end

      def delete_one(coll)
        coll.delete_one(filter)
      end

      def drop(coll)
        coll.drop
      end

      def rename(coll)
        coll.client.use(:admin).command({
          renameCollection: "#{coll.database.name}.#{coll.name}", 
          to: "#{coll.database.name}.#{arguments['to']}"
        })
      end

      def arguments
        @spec['arguments']
      end

      def document
        arguments['document']
      end

      def filter
        arguments['filter']
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
mongo-2.12.4 spec/runners/change_streams/operation.rb
mongo-2.11.6 spec/support/change_streams/operation.rb
mongo-2.12.3 spec/runners/change_streams/operation.rb
mongo-2.12.2 spec/runners/change_streams/operation.rb
mongo-2.10.5 spec/support/change_streams/operation.rb
mongo-2.11.5 spec/support/change_streams/operation.rb
mongo-2.12.1 spec/runners/change_streams/operation.rb
mongo-2.12.0.rc0 spec/runners/change_streams/operation.rb
mongo-2.11.4 spec/support/change_streams/operation.rb
mongo-2.10.4 spec/support/change_streams/operation.rb
mongo-2.11.3 spec/support/change_streams/operation.rb
mongo-2.11.2 spec/support/change_streams/operation.rb
mongo-2.11.1 spec/support/change_streams/operation.rb
mongo-2.10.3 spec/support/change_streams/operation.rb
mongo-2.11.0 spec/support/change_streams/operation.rb
mongo-2.10.2 spec/support/change_streams/operation.rb
mongo-2.11.0.rc0 spec/support/change_streams/operation.rb
mongo-2.10.1 spec/support/change_streams/operation.rb
mongo-2.10.0 spec/support/change_streams/operation.rb
mongo-2.10.0.rc0 spec/support/change_streams/operation.rb