Permalink
Browse files

initial support for belongs_to and has_many; implemented validates_un…

…iqueness_of

Signed-off-by: David A. Cuadrado <krawek@gmail.com>
  • Loading branch information...
1 parent 0dc317a commit 6b0ea3c3daf68f6827e181f70bfc3d4c01bb7776 @dcu dcu committed Jul 6, 2009
Showing with 159 additions and 94 deletions.
  1. +1 −0 .gitignore
  2. +6 −5 lib/mongomapper.rb
  3. +40 −36 lib/mongomapper/document.rb
  4. +85 −42 lib/mongomapper/embedded_document.rb
  5. +11 −10 lib/mongomapper/key.rb
  6. +15 −0 lib/mongomapper/validations.rb
  7. +1 −1 test/test_key.rb
View
@@ -3,3 +3,4 @@
coverage
rdoc
pkg
+*~
View
@@ -18,6 +18,7 @@
require dir + 'serialization'
require dir + 'callbacks'
require dir + 'observing'
+require dir + 'validations'
require dir + 'embedded_document'
require dir + 'document'
@@ -29,20 +30,20 @@ def initialize(document)
super("Validation failed: #{@document.errors.full_messages.join(", ")}")
end
end
-
+
def self.connection
@@connection ||= XGen::Mongo::Driver::Mongo.new
end
-
+
def self.connection=(new_connection)
@@connection = new_connection
end
-
+
def self.database=(name)
@@database = MongoMapper.connection.db(name)
end
-
+
def self.database
@@database
end
-end
+end
@@ -1,4 +1,4 @@
-module MongoMapper
+module MongoMapper
module Document
def self.included(model)
model.class_eval do
@@ -9,48 +9,48 @@ def self.included(model)
include SaveWithValidation
include RailsCompatibility
extend ClassMethods
-
+
key :_id, String
key :created_at, Time
key :updated_at, Time
end
end
-
+
module ClassMethods
def find(*args)
options = args.extract_options!
-
+
case args.first
when :first then find_first(options)
when :last then find_last(options)
when :all then find_every(options)
else find_from_ids(args)
end
end
-
+
def first(options={})
find_first(options)
end
-
+
def last(options={})
find_last(options)
end
-
+
def all(options={})
find_every(options)
end
-
+
def find_by_id(id)
if doc = collection.find_first({:_id => id})
new(doc)
end
end
-
+
# TODO: remove the rescuing when ruby driver works correctly
def count(conditions={})
collection.count(conditions)
end
-
+
def create(*docs)
instances = []
docs.flatten.each do |attrs|
@@ -59,12 +59,12 @@ def create(*docs)
end
instances.size == 1 ? instances[0] : instances
end
-
+
# For updating single document
# Person.update(1, {:foo => 'bar'})
#
# For updating multiple documents at once:
- # Person.update({'1' => {:foo => 'bar'}, '2' => {:baz => 'wick'}})
+ # Person.update({'1' => {:foo => 'bar'}, '2' => {:baz => 'wick'}})
def update(*args)
updating_multiple = args.length == 1
if updating_multiple
@@ -74,23 +74,23 @@ def update(*args)
update_single(id, attributes)
end
end
-
+
def delete(*ids)
collection.remove(:_id => {'$in' => ids.flatten})
end
-
+
def delete_all(conditions={})
collection.remove(conditions)
end
-
+
def destroy(*ids)
find_some(ids.flatten).each(&:destroy)
end
-
+
def destroy_all(conditions={})
find(:all, :conditions => conditions).each(&:destroy)
end
-
+
def connection(mongo_connection=nil)
if mongo_connection.nil?
@connection ||= MongoMapper.connection
@@ -99,7 +99,7 @@ def connection(mongo_connection=nil)
end
@connection
end
-
+
def database(name=nil)
if name.nil?
@database ||= MongoMapper.database
@@ -108,7 +108,7 @@ def database(name=nil)
end
@database
end
-
+
def collection(name=nil)
if name.nil?
@collection ||= database.collection(self.to_s.demodulize.tableize)
@@ -117,7 +117,11 @@ def collection(name=nil)
end
@collection
end
-
+
+ def validates_uniqueness_of(*args)
+ add_validations(args, MongoMapper::Validations::ValidatesUniquenessOf)
+ end
+
private
def find_every(options)
criteria, options = FinderOptions.new(options).to_a
@@ -153,15 +157,15 @@ def find_from_ids(*ids)
find_some(ids)
end
end
-
+
def update_single(id, attrs)
if id.blank? || attrs.blank? || !attrs.is_a?(Hash)
raise ArgumentError, "Updating a single document requires an id and a hash of attributes"
end
-
+
find(id).update_attributes(attrs)
end
-
+
def update_multiple(docs)
unless docs.is_a?(Hash)
raise ArgumentError, "Updating multiple documents takes 1 argument and it must be hash"
@@ -171,43 +175,43 @@ def update_multiple(docs)
instances
end
end
-
+
module InstanceMethods
def collection
self.class.collection
end
-
+
def new?
read_attribute('_id').blank? || self.class.find_by_id(id).blank?
end
-
+
def save
create_or_update
end
def save!
create_or_update || raise(DocumentNotValid.new(self))
end
-
+
def update_attributes(attrs={})
self.attributes = attrs
save
self
end
-
+
def destroy
collection.remove(:_id => id) unless new?
freeze
end
-
+
def ==(other)
other.is_a?(self.class) && id == other.id
end
-
+
def id
read_attribute('_id')
end
-
+
private
def create_or_update
result = new? ? create : update
@@ -219,24 +223,24 @@ def create
update_timestamps
save_to_collection
end
-
+
def update
update_timestamps
save_to_collection
end
-
+
def save_to_collection
collection.save(attributes.merge!(embedded_association_attributes))
end
-
+
def update_timestamps
write_attribute('created_at', Time.now.utc) if new?
write_attribute('updated_at', Time.now.utc)
end
-
+
def generate_id
XGen::Mongo::Driver::ObjectID.new
end
end
end # Document
-end # MongoMapper
+end # MongoMapper
Oops, something went wrong.

0 comments on commit 6b0ea3c

Please sign in to comment.