Sha256: f8966c25a9c378212defb9be9f3d9f0d1718fd79354f8f2890bddacfaa96739d

Contents?: true

Size: 981 Bytes

Versions: 2

Compression:

Stored size: 981 Bytes

Contents

begin
  require "em-mongo"
rescue LoadError => error
  raise "Missing EM-Synchrony dependency: gem install em-mongo"
end

module EM
  module Mongo

    class Connection
      def initialize(host = DEFAULT_IP, port = DEFAULT_PORT, timeout = nil, opts = {})
        f = Fiber.current

        @em_connection = EMConnection.connect(host, port, timeout, opts)
        @db = {}

        # establish connection before returning
        EM.next_tick { f.resume }
        Fiber.yield
      end
    end

    class Collection

      alias :afind :find
      def find(selector={}, opts={})

        f = Fiber.current
        cb = proc { |res| f.resume(res) }

        skip  = opts.delete(:skip) || 0
        limit = opts.delete(:limit) || 0

        @connection.find(@name, skip, limit, selector, nil, &cb)
        Fiber.yield
      end

      alias :afirst :first
      def first(selector={}, opts={})
        opts[:limit] = 1
        find(selector, opts).first
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
em-synchrony-0.3.0.beta.1 lib/em-synchrony/em-mongo.rb
em-synchrony-0.2.0 lib/em-synchrony/em-mongo.rb