Sha256: 66ba7e66e4ae5cdb847ba0b72483f931846c7ae4122f2445c3726b30ebb44c79

Contents?: true

Size: 989 Bytes

Versions: 6

Compression:

Stored size: 989 Bytes

Contents

module EM::Mongo
  class Collection
    attr_accessor :connection

    def initialize(db, ns, connection = nil)
      @db = db || "db"
      @ns = ns || "ns"
      @name = [@db,@ns].join('.')
      @connection = connection || EM::Mongo::Connection.new
    end

    def find(selector={}, opts={}, &blk)
      raise "find requires a block" if not block_given?

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

      @connection.find(@name, skip, limit, selector, nil, &blk)
    end

    def first(selector={}, opts={}, &blk)
      opts[:limit] = 1
      find(selector, opts) do |res|
        yield res.first
      end
    end

    def insert(obj)
      obj['_id'] ||= EM::Mongo::Util.unique_id
      @connection.insert(@name, obj)
      obj
    end

    def update(selector, updater, opts={})
      @connection.update(@name, selector, updater, opts)
      true
    end

    def remove(obj = {})
      @connection.delete(@name, obj)
      true
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
em-mongo-0.2.11 lib/em-mongo/collection.rb
em-mongo-0.2.10 lib/em-mongo/collection.rb
em-mongo-0.2.9 lib/em-mongo/collection.rb
em-mongo-0.2.8 lib/em-mongo/collection.rb
em-mongo-0.2.7 lib/em-mongo/collection.rb
em-mongo-0.2.6 lib/em-mongo/collection.rb