public
Created

Criteria

  • Download Gist
criteria.rb
Ruby
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
class Criteria
 
def initialize(klass)
@klass = klass
end
 
def criteria
@criteria ||= {:conditions => {}}
end
 
def where(args)
criteria[:conditions].merge!(args)
self
end
 
def limit(limit)
criteria[:limit] = limit
self
end
 
def each(&block)
@klass.collection.find(
criteria[:conditions], {:limit => criteria[:limit]}
).each(&block)
end
 
end
user.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
require 'mongo'
require File.expand_path 'criteria'
 
class User
 
def self.collection
@collection ||= Mongo::Connection.new['criteria']['users']
end
 
def self.limit(*args)
Criteria.new(self).limit(*args)
end
 
def self.where(*args)
Criteria.new(self).where(*args)
end
 
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.