File: prev.rb

package info (click to toggle)
ruby-em-mongo 0.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 320 kB
  • sloc: ruby: 2,728; makefile: 2
file content (53 lines) | stat: -rw-r--r-- 1,517 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module EM
  module Mongo
    class Collection

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

        new_find(selector, opts).defer_as_a.callback do |docs|
          blk.call(docs)
        end
      end

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

    class Connection

      def insert(collection_name, documents)
        db_name, col_name = db_and_col_name(collection_name)
        db(db_name).collection(col_name).insert(documents)
      end

      def update(collection_name, selector, document, options={})
        db_name, col_name = db_and_col_name(collection_name)
        db(db_name).collection(col_name).update(selector, document, options)
      end

      def delete(collection_name, selector)
        db_name, col_name = db_and_col_name(collection_name)
        db(db_name).collection(col_name).remove(selector)
      end

      def find(collection_name, skip, limit, order, query, fields, &blk)
        db_name, col_name = db_and_col_name(collection_name)
        db(db_name).collection(col_name).find(query, :skip=>skip,:limit=>limit,:order=>order,:fields=>fields).defer_as_a.callback do |docs|
          yield docs if block_given?
        end
      end

      def db_and_col_name(full_name)
        parts = full_name.split(".")
        [ parts.shift, parts.join(".") ]
      end

    end
  end
end