Sha256: 2b9decb81d211d23681f2139150e027812c0fc6da197d687a988799f671efdeb

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

module Minimongo
  module Query

    # # # # # # # # # # # # #
    # This is the tiniest possible implementation of a mongodb library.
    # Instead of doing $db[:models].find, you can now do find(:models),
    # and there's some extra short cuts like first, last, all and count.
    #
    # Intended use is with tests, especially Fugroup/Futest, but can also
    # be used for full fledged applications if you don't want an ORM.
    #
    # If you need models for Minimongo, check out https://github.com/fugroup/modelize
    #
    # If you need an ORM, check out https://github.com/fugroup/mongocore
    #

    # Creates a BSON::ObjectId from a string, or a new one
    # Use with: oid(string), or oid, oid(:new), oid(nil) to create a new BSON::ObjectId
    def oid(v = nil); BSON::ObjectId.from_string(v) rescue BSON::ObjectId.new; end

    # Find, insert, update, delete
    [:find, :insert, :update, :delete].each{|m| class_eval %Q{def #{m}(*g); Minimongo.db[g.shift].#{m != :find ? "#{m}_one" : m}(*g); end}}

    # First, last, count, all
    def first(*g); find(*g).limit(-1).first; end
    def last(*g); find(*g).limit(-1).sort(:$natural => -1).first; end
    def count(*g); find(*g).count; end
    def all(*g); find(*g).to_a; end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
minimongo-0.1.0 lib/minimongo/query.rb