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
- #to_key ⇒ Object
- #to_model ⇒ Object
- #to_param ⇒ 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 |
# 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 extend Validations alias id _id end end |
Instance Method Details
#==(other) ⇒ Object
53 54 55 56 |
# File 'lib/mongo_doc/document.rb', line 53 def ==(other) return false unless self.class === other self.class._attributes.all? {|var| self.send(var) == other.send(var)} end |
#_collection ⇒ Object
45 46 47 |
# File 'lib/mongo_doc/document.rb', line 45 def _collection _root and _root._collection or self.class.collection end |
#destroyed? ⇒ Boolean
58 59 60 |
# File 'lib/mongo_doc/document.rb', line 58 def destroyed? _id.nil? end |
#initialize(attrs = {}) ⇒ Object
49 50 51 |
# File 'lib/mongo_doc/document.rb', line 49 def initialize(attrs = {}) self.attributes = attrs if attrs end |
#new_record? ⇒ Boolean
62 63 64 |
# File 'lib/mongo_doc/document.rb', line 62 def new_record? _id.nil? end |
#persisted? ⇒ Boolean
66 67 68 |
# File 'lib/mongo_doc/document.rb', line 66 def persisted? _id.present? end |
#remove ⇒ Object
70 71 72 73 |
# File 'lib/mongo_doc/document.rb', line 70 def remove raise UnsupportedOperation.new('Document#remove is not supported for embedded documents') if _root remove_document end |
#remove_document ⇒ Object
75 76 77 78 |
# File 'lib/mongo_doc/document.rb', line 75 def remove_document return _root.remove_document if _root _remove end |
#save(validate = true) ⇒ Object
80 81 82 83 84 |
# File 'lib/mongo_doc/document.rb', line 80 def save(validate = true) return _root.save(validate) if _root return _save(false) unless validate and not valid? false end |
#save! ⇒ Object
86 87 88 89 90 |
# File 'lib/mongo_doc/document.rb', line 86 def save! return _root.save! if _root raise DocumentInvalidError unless valid? _save(true) end |
#to_bson(*args) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/mongo_doc/document.rb', line 92 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 |
#to_key ⇒ Object
104 105 106 |
# File 'lib/mongo_doc/document.rb', line 104 def to_key persisted? ? [_id] : nil end |
#to_model ⇒ Object
108 109 110 |
# File 'lib/mongo_doc/document.rb', line 108 def to_model self end |
#to_param ⇒ Object
112 113 114 |
# File 'lib/mongo_doc/document.rb', line 112 def to_param persisted? ? _id.to_s : nil end |
#update(attrs, safe = false) ⇒ Object
Update without checking validations. The Document
will be saved without validations if it is a new record.
131 132 133 134 135 136 137 138 139 |
# File 'lib/mongo_doc/document.rb', line 131 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
116 117 118 119 120 121 |
# File 'lib/mongo_doc/document.rb', line 116 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
123 124 125 126 127 128 |
# File 'lib/mongo_doc/document.rb', line 123 def update_attributes!(attrs) self.attributes = attrs return save! if new_record? raise DocumentInvalidError unless valid? _update_attributes(converted_attributes(attrs), true) end |