Skip to content

Commit

Permalink
Moving finders into their own module
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Oct 1, 2009
1 parent 094c530 commit a4d6b69
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 51 deletions.
7 changes: 4 additions & 3 deletions lib/mongoid.rb
@@ -1,16 +1,16 @@
# Copyright (c) 2009 Durran Jordan
#
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Expand Down Expand Up @@ -38,6 +38,7 @@
require "mongoid/extensions/array/conversions"
require "mongoid/extensions/object/conversions"
require "mongoid/extensions"
require "mongoid/finders"
require "mongoid/document"

module Mongoid
Expand Down
49 changes: 1 addition & 48 deletions lib/mongoid/document.rb
Expand Up @@ -2,9 +2,7 @@ module Mongoid #:nodoc:
class Document #:nodoc:
include ActiveSupport::Callbacks
include Validatable

AGGREGATE_REDUCE = "function(obj, prev) { prev.count++; }"
GROUP_BY_REDUCE = "function(obj, prev) { prev.group.push(obj); }"
extend Finders

attr_reader :attributes, :parent

Expand Down Expand Up @@ -50,38 +48,6 @@ def fields(*names)
end
end

# Find all Documents in several ways.
# Model.find(:first, :attribute => "value")
# Model.find(:all, :attribute => "value")
def find(*args)
type, selector = args[0], args[1]
case type
when :all then find_all(selector[:conditions])
when :first then find_first(selector[:conditions])
else find_first(Mongo::ObjectID.from_string(type.to_s))
end
end

# Find a single Document given the passed selector, which is a Hash of attributes that
# must match the Document in the database exactly.
def find_first(selector = nil)
new(collection.find_one(selector))
end

# Find all Documents given the passed selector, which is a Hash of attributes that
# must match the Document in the database exactly.
def find_all(selector = nil)
collection.find(selector).collect { |doc| new(doc) }
end

# Find all Documents given the supplied criteria, grouped by the fields
# provided.
def group_by(fields, selector)
collection.group(fields, selector, { :group => [] }, GROUP_BY_REDUCE).collect do |docs|
group!(docs)
end
end

# Create a one-to-many association between Documents.
def has_many(association_name)
add_association(:has_many, association_name.to_s.classify, association_name)
Expand All @@ -98,19 +64,6 @@ def index(name, options = { :unique => false })
collection.create_index(name, options)
end

# Find all documents in paginated fashion given the supplied arguments.
# If no parameters are passed just default to offset 0 and limit 20.
def paginate(params = {})
WillPaginate::Collection.create(
params[:page] || 1,
params[:per_page] || 20,
0) do |pager|
results = collection.find(params[:conditions], { :limit => pager.per_page, :offset => pager.offset })
pager.total_entries = results.count
pager.replace(results.collect { |doc| new(doc) })
end
end

end

# Get the Mongo::Collection associated with this Document.
Expand Down
53 changes: 53 additions & 0 deletions lib/mongoid/finders.rb
@@ -0,0 +1,53 @@
module Mongoid
module Finders

AGGREGATE_REDUCE = "function(obj, prev) { prev.count++; }"
GROUP_BY_REDUCE = "function(obj, prev) { prev.group.push(obj); }"

# Find all Documents in several ways.
# Model.find(:first, :attribute => "value")
# Model.find(:all, :attribute => "value")
def find(*args)
type, selector = args[0], args[1]
case type
when :all then find_all(selector[:conditions])
when :first then find_first(selector[:conditions])
else find_first(Mongo::ObjectID.from_string(type.to_s))
end
end

# Find a single Document given the passed selector, which is a Hash of attributes that
# must match the Document in the database exactly.
def find_first(selector = nil)
new(collection.find_one(selector))
end

# Find all Documents given the passed selector, which is a Hash of attributes that
# must match the Document in the database exactly.
def find_all(selector = nil)
collection.find(selector).collect { |doc| new(doc) }
end

# Find all Documents given the supplied criteria, grouped by the fields
# provided.
def group_by(fields, selector)
collection.group(fields, selector, { :group => [] }, GROUP_BY_REDUCE).collect do |docs|
group!(docs)
end
end

# Find all documents in paginated fashion given the supplied arguments.
# If no parameters are passed just default to offset 0 and limit 20.
def paginate(params = {})
WillPaginate::Collection.create(
params[:page] || 1,
params[:per_page] || 20,
0) do |pager|
results = collection.find(params[:conditions], { :limit => pager.per_page, :offset => pager.offset })
pager.total_entries = results.count
pager.replace(results.collect { |doc| new(doc) })
end
end

end
end

0 comments on commit a4d6b69

Please sign in to comment.