lib/mongodoc/document.rb in mongodoc-0.2.1 vs lib/mongodoc/document.rb in mongodoc-0.2.2
- old
+ new
@@ -2,12 +2,14 @@
require 'mongodoc/query'
require 'mongodoc/attributes'
require 'mongodoc/criteria'
require 'mongodoc/finders'
require 'mongodoc/named_scope'
+require 'mongodoc/validations/macros'
module MongoDoc
+ class UnsupportedOperation < RuntimeError; end
class DocumentInvalidError < RuntimeError; end
class NotADocumentError < RuntimeError; end
module Document
@@ -15,14 +17,14 @@
klass.class_eval do
include Attributes
extend ClassMethods
extend Finders
extend NamedScope
- include Validatable
+ include ::Validatable
+ extend Validations::Macros
alias :id :_id
- alias :to_param :_id
end
end
def initialize(attrs = {})
self.attributes = attrs
@@ -32,14 +34,15 @@
return false unless self.class === other
self.class._attributes.all? {|var| self.send(var) == other.send(var)}
end
def attributes
- self.class._attributes.inject({}) do |hash, attr|
+ hash = {}
+ self.class._attributes.each do |attr|
hash[attr] = send(attr)
- hash
end
+ hash
end
def attributes=(attrs)
attrs.each do |key, value|
send("#{key}=", value)
@@ -48,10 +51,20 @@
def new_record?
_id.nil?
end
+ def remove
+ raise UnsupportedOperation.new('Document#remove is not supported for embedded documents') if _root
+ remove_document
+ end
+
+ def remove_document
+ return _root.remove_document if _root
+ _remove
+ end
+
def save(validate = true)
return _root.save(validate) if _root
return _save(false) unless validate and not valid?
false
end
@@ -62,20 +75,25 @@
_save(true)
end
def to_bson(*args)
{MongoDoc::BSON::CLASS_KEY => self.class.name}.tap do |bson_hash|
- bson_hash['_id'] = _id unless _id.nil?
+ bson_hash['_id'] = _id unless new_record?
self.class._attributes.each do |name|
bson_hash[name.to_s] = send(name).to_bson(args)
end
end
end
+ def to_param
+ _id.to_s
+ end
+
def update_attributes(attrs)
strict = attrs.delete(:__strict__)
self.attributes = attrs
+ return save if new_record?
return false unless valid?
if strict
_strict_update_attributes(_path_to_root(self, attrs), false)
else
_naive_update_attributes(_path_to_root(self, attrs), false)
@@ -83,10 +101,11 @@
end
def update_attributes!(attrs)
strict = attrs.delete(:__strict__)
self.attributes = attrs
+ return save! if new_record?
raise DocumentInvalidError unless valid?
if strict
_strict_update_attributes(_path_to_root(self, attrs), true)
else
_naive_update_attributes(_path_to_root(self, attrs), true)
@@ -110,50 +129,45 @@
self.to_s.tableize.gsub('/', '.')
end
def create(attrs = {})
instance = new(attrs)
- _create(instance, false) if instance.valid?
+ instance.save(false)
instance
end
def create!(attrs = {})
instance = new(attrs)
- raise MongoDoc::DocumentInvalidError unless instance.valid?
- _create(instance, true)
+ instance.save!(true)
instance
end
-
- protected
-
- def _create(instance, safe)
- instance.send(:notify_before_save_observers)
- instance._id = collection.insert(instance, :safe => safe)
- instance.send(:notify_save_success_observers)
- instance._id
- rescue Mongo::MongoDBError => e
- instance.send(:notify_save_failed_observers)
- raise e
- end
end
protected
def _collection
self.class.collection
end
def _naive_update_attributes(attrs, safe)
return _root.send(:_naive_update_attributes, attrs, safe) if _root
- _collection.update({'_id' => self._id}, MongoDoc::Query.set_modifier(attrs), :safe => safe)
+ _update({}, attrs, safe)
end
+ def _remove
+ _collection.remove({'_id' => _id})
+ end
+
def _strict_update_attributes(attrs, safe, selector = {})
- return _root.send(:_strict_update_attributes, attrs, safe, _selector_path_to_root('_id' => _id)) if _root
- _collection.update({'_id' => _id}.merge(selector), MongoDoc::Query.set_modifier(attrs), :safe => safe)
+ return _root.send(:_strict_update_attributes, attrs, safe, _path_to_root(self, '_id' => _id)) if _root
+ _update(selector, attrs, safe)
end
+ def _update(selector, data, safe)
+ _collection.update({'_id' => _id}.merge(selector), MongoDoc::Query.set_modifier(data), :safe => safe)
+ end
+
def _save(safe)
notify_before_save_observers
self._id = _collection.save(self, :safe => safe)
notify_save_success_observers
self._id
@@ -195,8 +209,7 @@
end
def notify_save_failed_observers
save_observers.each {|obs| obs.save_failed_callback(self) }
end
-
end
end