Module: MongoDoc::Document
- Defined in:
- lib/mongo_doc/document.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #_collection ⇒ Object
- #destroyed? ⇒ Boolean
- #initialize(attrs = {}) ⇒ Object
- #new_record? ⇒ Boolean
- #persisted? ⇒ Boolean
- #remove ⇒ Object
- #remove_document ⇒ Object
- #save(validate = true) ⇒ Object
- #save! ⇒ Object
- #to_bson(*args) ⇒ Object
-
#update(attrs, safe = false) ⇒ Object
Update without checking validations.
- #update_attributes(attrs) ⇒ Object
- #update_attributes!(attrs) ⇒ Object
Class Method Details
.included(klass) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mongo_doc/document.rb', line 23 def self.included(klass) klass.class_eval do include Attributes include Root extend PolymorphicCollection extend Associations extend ClassMethods extend Criteria extend Finders extend Index extend Scope extend Timestamps extend References extend ReferencesMany include ::ActiveModel::Validations extend ::ActiveModel::Naming include ::ActiveModel::Conversion extend Validations alias id _id end end |
Instance Method Details
#==(other) ⇒ Object
54 55 56 57 |
# File 'lib/mongo_doc/document.rb', line 54 def ==(other) return false unless self.class === other self.class._attributes.all? {|var| self.send(var) == other.send(var)} end |
#_collection ⇒ Object
46 47 48 |
# File 'lib/mongo_doc/document.rb', line 46 def _collection _root and _root._collection or self.class.collection end |
#destroyed? ⇒ Boolean
59 60 61 |
# File 'lib/mongo_doc/document.rb', line 59 def destroyed? _id.nil? end |
#initialize(attrs = {}) ⇒ Object
50 51 52 |
# File 'lib/mongo_doc/document.rb', line 50 def initialize(attrs = {}) self.attributes = attrs if attrs end |
#new_record? ⇒ Boolean
63 64 65 |
# File 'lib/mongo_doc/document.rb', line 63 def new_record? _id.nil? end |
#persisted? ⇒ Boolean
67 68 69 |
# File 'lib/mongo_doc/document.rb', line 67 def persisted? _id.present? end |
#remove ⇒ Object
71 72 73 74 |
# File 'lib/mongo_doc/document.rb', line 71 def remove raise UnsupportedOperation.new('Document#remove is not supported for embedded documents') if _root remove_document end |
#remove_document ⇒ Object
76 77 78 79 |
# File 'lib/mongo_doc/document.rb', line 76 def remove_document return _root.remove_document if _root _remove end |
#save(validate = true) ⇒ Object
81 82 83 84 85 |
# File 'lib/mongo_doc/document.rb', line 81 def save(validate = true) return _root.save(validate) if _root return _save(false) unless validate and not valid? false end |
#save! ⇒ Object
87 88 89 90 91 |
# File 'lib/mongo_doc/document.rb', line 87 def save! return _root.save! if _root raise DocumentInvalidError unless valid? _save(true) end |
#to_bson(*args) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/mongo_doc/document.rb', line 93 def to_bson(*args) {MongoDoc::BSON::CLASS_KEY => self.class.name}.tap do |bson_hash| bson_hash['_id'] = _id unless new_record? self.class._attributes.each do |name| bson_hash[name.to_s] = send(name).to_bson(args) end if MongoDoc::Configuration.dynamic_attributes && dynamic_attributes.any? bson_hash.merge!(dynamic_attributes) end end end |
#update(attrs, safe = false) ⇒ Object
Update without checking validations. The Document
will be saved without validations if it is a new record.
120 121 122 123 124 125 126 127 128 |
# File 'lib/mongo_doc/document.rb', line 120 def update(attrs, safe = false) self.attributes = attrs if new_record? _root.send(:_save, safe) if _root _save(safe) else _update_attributes(converted_attributes(attrs), safe) end end |
#update_attributes(attrs) ⇒ Object
105 106 107 108 109 110 |
# File 'lib/mongo_doc/document.rb', line 105 def update_attributes(attrs) self.attributes = attrs return save if new_record? return false unless valid? _update_attributes(converted_attributes(attrs), false) end |
#update_attributes!(attrs) ⇒ Object
112 113 114 115 116 117 |
# File 'lib/mongo_doc/document.rb', line 112 def update_attributes!(attrs) self.attributes = attrs return save! if new_record? raise DocumentInvalidError unless valid? _update_attributes(converted_attributes(attrs), true) end |