Skip to content

Commit

Permalink
Pulling in master
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Mar 16, 2010
2 parents 52d04a0 + df8c1d1 commit 9b4f333
Show file tree
Hide file tree
Showing 29 changed files with 463 additions and 119 deletions.
2 changes: 1 addition & 1 deletion .watchr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ watch("spec/.*/*_spec\.rb") do |match|
spec(match[0])
end

watch('lib/(.*/.*)\.rb') do |match|
watch("lib/(.*/.*)\.rb") do |match|
p match[1]
spec("spec/unit/#{match[1]}_spec.rb")
end
9 changes: 1 addition & 8 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,13 @@ Mongoid is an ODM (Object-Document-Mapper) framework for MongoDB in Ruby.

* {Mongoid on Pivotal Tracker}[http://www.pivotaltracker.com/projects/27482]
* {Mongoid Google Group}[http://groups.google.com/group/mongoid]
* {Mongoid on CI Joe}[http://mongoid.org:4444/]
* {Mongoid on CI Joe}[http://ci.mongoid.org/]
* {Mongoid Website and Documentation}[http://mongoid.org]

== Compatibility

Mongoid is developed against Ruby 1.8.6, 1.8.7, 1.9.1, 1.9.2

Note API changes will be frequent until 1.0, releases will be
frequent, and backwards compatibility will not be supported. Do not
expect the gem to be of full production quality until that
point. However, once 1.0 is released I will be providing backwards
compatibility through each minor version upgrade, as well as detailed
release notes and documentation with each release.

= Documentation

Please see the new Mongoid website for up-to-date documentation:
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ begin

gem.add_dependency("activemodel", ">= 3.0.pre")
gem.add_dependency("will_paginate", ">= 3.0.pre")
gem.add_dependency("mongo", ">= 0.18.2")
gem.add_dependency("mongo", ">= 0.19.1")

gem.add_development_dependency("rspec", ">= 1.2.9")
gem.add_development_dependency("mocha", ">= 0.9.8")
Expand Down
3 changes: 2 additions & 1 deletion lib/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

gem "activemodel", ">= 3.0.pre"
gem "will_paginate", ">= 3.0.pre"
gem "mongo", ">= 0.18.2"
gem "mongo", ">= 0.19.1"

require "delegate"
require "observer"
Expand Down Expand Up @@ -69,6 +69,7 @@
require "mongoid/memoization"
require "mongoid/named_scope"
require "mongoid/scope"
require "mongoid/state"
require "mongoid/timestamps"
require "mongoid/validations"
require "mongoid/versioning"
Expand Down
45 changes: 30 additions & 15 deletions lib/mongoid/associations/has_many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ class HasMany

# Appends the object to the +Array+, setting its parent in
# the process.
def <<(*objects)
objects.flatten.each do |object|
object.parentize(@parent, @association_name)
@target << object
object.notify
def <<(*documents)
documents.flatten.each do |doc|
doc.parentize(@parent, @association_name)
@target << doc
doc.notify
end
end

Expand All @@ -22,9 +22,9 @@ def <<(*objects)
# Clears the association, and notifies the parents of the removal.
def clear
unless @target.empty?
object = @target.first
object.changed(true)
object.notify_observers(object, true)
document = @target.first
document.changed(true)
document.notify_observers(document, true)
@target.clear
end
end
Expand All @@ -37,11 +37,11 @@ def clear
#
# The newly created Document.
def build(attrs = {}, type = nil)
object = type ? type.instantiate : @klass.instantiate
object.parentize(@parent, @association_name)
object.write_attributes(attrs)
@target << object
object
document = type ? type.instantiate : @klass.instantiate
document.parentize(@parent, @association_name)
document.write_attributes(attrs)
@target << document
document
end

# Creates a new Document and adds it to the association collection. The
Expand All @@ -51,12 +51,28 @@ def build(attrs = {}, type = nil)
#
# Returns:
#
# Rhe newly created Document.
# The newly created Document.
def create(attrs = {}, type = nil)
object = build(attrs, type)
object.run_callbacks(:create) { object.save }; object
end

# Creates a new Document and adds it to the association collection. The
# document created will be of the same class as the others in the
# association, and the attributes will be passed into the constructor and
# the new object will then be saved. If validation fails an error will
# get raised.
#
# Returns:
#
# The newly created Document.
def create!(attrs = {}, type = nil)
document = create(attrs, type)
errors = document.errors
raise Errors::Validations.new(errors) unless errors.empty?
document
end

# Finds a document in this association.
#
# If :all is passed, returns all the documents
Expand Down Expand Up @@ -173,7 +189,6 @@ def update(children, parent, options)
instantiate(parent, options)
end
end

end
end
end
2 changes: 1 addition & 1 deletion lib/mongoid/commands/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Delete
#
# doc: A new +Document+ that is going to be deleted.
def self.execute(doc)
delete(doc)
doc.destroyed = true if delete(doc)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/commands/destroy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Destroy
#
# doc: A new +Document+ that is going to be destroyed.
def self.execute(doc)
doc.run_callbacks(:destroy) { delete(doc) }
doc.run_callbacks(:destroy) { doc.destroyed = true if delete(doc) }
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/mongoid/components.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Components #:nodoc
include Mongoid::Indexes
include Mongoid::Matchers
include Mongoid::Memoization
include Mongoid::State
include Mongoid::Validations
include Observable
extend ActiveModel::Translation
Expand Down
24 changes: 24 additions & 0 deletions lib/mongoid/contexts/enumerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,35 @@ def aggregate
counts
end

# Get the average value for the supplied field.
#
# Example:
#
# <tt>context.avg(:age)</tt>
#
# Returns:
#
# A numeric value that is the average.
def avg(field)
total = sum(field)
total ? (total.to_f / count) : nil
end

# Gets the number of documents in the array. Delegates to size.
def count
@count ||= documents.size
end

# Gets an array of distinct values for the supplied field across the
# entire array or the susbset given the criteria.
#
# Example:
#
# <tt>context.distinct(:title)</tt>
def distinct(field)
execute.collect { |doc| doc.send(field) }.uniq
end

# Enumerable implementation of execute. Returns matching documents for
# the selector, and adds options if supplied.
#
Expand Down
35 changes: 31 additions & 4 deletions lib/mongoid/contexts/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@ def aggregate
klass.collection.group(options[:fields], selector, { :count => 0 }, Javascript.aggregate, true)
end

# Get the average value for the supplied field.
#
# This will take the internally built selector and options
# and pass them on to the Ruby driver's +group()+ method on the collection. The
# collection itself will be retrieved from the class provided, and once the
# query has returned it will provided a grouping of keys with averages.
#
# Example:
#
# <tt>context.avg(:age)</tt>
#
# Returns:
#
# A numeric value that is the average.
def avg(field)
total = sum(field)
total ? (total / count) : nil
end

# Determine if the context is empty or blank given the criteria. Will
# perform a quick has_one asking only for the id.
#
Expand All @@ -48,6 +67,16 @@ def count
@count ||= klass.collection.find(selector, process_options).count
end

# Gets an array of distinct values for the supplied field across the
# entire collection or the susbset given the criteria.
#
# Example:
#
# <tt>context.distinct(:title)</tt>
def distinct(field)
klass.collection.distinct(field, selector)
end

# Execute the context. This will take the selector and options
# and pass them on to the Ruby driver's +find()+ method on the collection. The
# collection itself will be retrieved from the class provided, and once the
Expand Down Expand Up @@ -87,8 +116,7 @@ def group
options[:fields],
selector,
{ :group => [] },
Javascript.group,
true
Javascript.group
).collect do |docs|
docs["group"] = docs["group"].collect do |attrs|
Mongoid::Factory.build(klass, attrs)
Expand Down Expand Up @@ -222,8 +250,7 @@ def grouped(start, field, reduce)
nil,
selector,
{ start => "start" },
reduce.gsub("[field]", field),
true
reduce.gsub("[field]", field)
)
collection.empty? ? nil : collection.first[start.to_s]
end
Expand Down
3 changes: 2 additions & 1 deletion lib/mongoid/criteria.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ class Criteria
include Enumerable

attr_reader :collection, :ids, :klass, :options, :selector

attr_accessor :documents

delegate \
:aggregate,
:avg,
:blank?,
:count,
:distinct,
:empty?,
:execute,
:first,
Expand Down
17 changes: 0 additions & 17 deletions lib/mongoid/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,23 +177,6 @@ def inspect
"#<#{self.class.name} _id: #{id}, #{attrs}>"
end

# Returns true is the +Document+ has not been persisted to the database,
# false if it has. This is determined by the variable @new_record
# and NOT if the object has an id.
def new_record?
@new_record == true
end

# Sets the new_record boolean - used after document is saved.
def new_record=(saved)
@new_record = saved
end

# Checks if the document has been saved to the database.
def persisted?
!new_record?
end

# Set the changed state of the +Document+ then notify observers that it has changed.
#
# Example:
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/finders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Finders #:nodoc:

# Delegate to the criteria methods that are natural for creating a new
# criteria.
[ :all_in, :any_in, :excludes, :limit, :max, :min,
[ :all_in, :any_in, :avg, :excludes, :limit, :max, :min,
:not_in, :only, :order_by, :skip, :sum, :where ].each do |name|
define_method(name) do |*args|
criteria.send(name, *args)
Expand Down
32 changes: 32 additions & 0 deletions lib/mongoid/state.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# encoding: utf-8
module Mongoid #:nodoc:
module State #:nodoc:
# Returns true if the +Document+ has not been persisted to the database,
# false if it has. This is determined by the variable @new_record
# and NOT if the object has an id.
def new_record?
@new_record == true
end

# Sets the new_record boolean - used after document is saved.
def new_record=(saved)
@new_record = saved
end

# Checks if the document has been saved to the database.
def persisted?
!new_record?
end

# Returns true if the +Document+ has been succesfully destroyed, and false if it hasn't.
# This is determined by the variable @destroyed and NOT by checking the database.
def destroyed?
@destroyed == true
end

# Sets the destroyed boolean - used after document is destroyed.
def destroyed=(destroyed)
@destroyed = destroyed && true
end
end
end
Loading

0 comments on commit 9b4f333

Please sign in to comment.