lib/plucky/query.rb in plucky-0.1.4 vs lib/plucky/query.rb in plucky-0.2

- old
+ new

@@ -28,71 +28,102 @@ return criteria.object_ids if keys.empty? criteria.object_ids = *keys self end - def find(opts={}) - update(opts).collection.find(criteria.to_hash, options.to_hash) + def per_page(limit=nil) + return @per_page || 25 if limit.nil? + @per_page = limit + self end + def paginate(opts={}) + page = opts.delete(:page) + limit = opts.delete(:per_page) || per_page + query = clone.update(opts) + total = query.count + paginator = Pagination::Paginator.new(total, page, limit) + query[:limit] = paginator.limit + query[:skip] = paginator.skip + query.all.tap do |docs| + docs.extend(Pagination::Decorator) + docs.paginator(paginator) + end + end + + def find_many(opts={}) + query = clone.update(opts) + query.collection.find(query.criteria.to_hash, query.options.to_hash) + end + def find_one(opts={}) - update(opts).collection.find_one(criteria.to_hash, options.to_hash) + query = clone.update(opts) + query.collection.find_one(query.criteria.to_hash, query.options.to_hash) end + def find(*ids) + if ids.size == 1 && !ids[0].is_a?(Array) + first(:_id => ids[0]) + else + all(:_id => ids.flatten) + end + end + def all(opts={}) - update(opts).find(to_hash).to_a + find_many(opts).to_a end def first(opts={}) - update(opts).find_one(to_hash) + find_one(opts) end def last(opts={}) - update(opts).reverse.find_one(to_hash) + clone.update(opts).reverse.find_one end def remove(opts={}) - update(opts).collection.remove(criteria.to_hash) + query = clone.update(opts) + query.collection.remove(query.criteria.to_hash) end def count(opts={}) - update(opts).find(to_hash).count + find_many(opts).count end def update(opts={}) opts.each { |key, value| self[key] = value } self end def fields(*args) - self[:fields] = args - self + clone.tap { |query| query.options[:fields] = args } end def limit(count=nil) - self[:limit] = count - self + clone.tap { |query| query.options[:limit] = count } end def reverse - self[:sort].map! { |s| [s[0], -s[1]] } unless self[:sort].nil? - self + clone.tap do |query| + query[:sort].map! do |s| + [s[0], -s[1]] + end unless query.options[:sort].nil? + end end def skip(count=nil) - self[:skip] = count - self + clone.tap { |query| query.options[:skip] = count } end def sort(*args) - self[:sort] = *args - self + clone.tap { |query| query.options[:sort] = *args } end def where(hash={}) - criteria.merge(CriteriaHash.new(hash)).to_hash.each { |key, value| self[key] = value } - self + clone.tap do |query| + query.criteria.merge!(CriteriaHash.new(hash)) + end end def [](key) key = key.to_sym if key.respond_to?(:to_sym) if OptionKeys.include?(key) @@ -110,11 +141,12 @@ @criteria[key] = value end end def merge(other) - merged = criteria.merge(other.criteria).to_hash.merge(options.to_hash.merge(other.options.to_hash)) - clone.update(merged) + merged_criteria = criteria.merge(other.criteria).to_hash + merged_options = options.merge(other.options).to_hash + clone.update(merged_criteria).update(merged_options) end def to_hash criteria.to_hash.merge(options.to_hash) end \ No newline at end of file