Module: MongoDoc::Document
- Defined in:
- lib/mongodoc/document.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #attributes ⇒ Object
- #attributes=(attrs) ⇒ Object
- #initialize(attrs = {}) ⇒ Object
- #new_record? ⇒ Boolean
- #remove ⇒ Object
- #remove_document ⇒ Object
- #save(validate = true) ⇒ Object
- #save! ⇒ Object
- #to_bson(*args) ⇒ Object
- #to_param ⇒ Object
- #update_attributes(attrs) ⇒ Object
- #update_attributes!(attrs) ⇒ Object
Class Method Details
.included(klass) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/mongodoc/document.rb', line 16 def self.included(klass) klass.class_eval do include Attributes extend ClassMethods extend Criteria extend Finders extend Scope include ::Validatable extend Validations::Macros alias :id :_id end end |
Instance Method Details
#==(other) ⇒ Object
34 35 36 37 |
# File 'lib/mongodoc/document.rb', line 34 def ==(other) return false unless self.class === other self.class._attributes.all? {|var| self.send(var) == other.send(var)} end |
#attributes ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/mongodoc/document.rb', line 39 def attributes hash = {} self.class._attributes.each do |attr| hash[attr] = send(attr) end hash end |
#attributes=(attrs) ⇒ Object
47 48 49 50 51 |
# File 'lib/mongodoc/document.rb', line 47 def attributes=(attrs) attrs.each do |key, value| send("#{key}=", value) end end |
#initialize(attrs = {}) ⇒ Object
30 31 32 |
# File 'lib/mongodoc/document.rb', line 30 def initialize(attrs = {}) self.attributes = attrs end |
#new_record? ⇒ Boolean
53 54 55 |
# File 'lib/mongodoc/document.rb', line 53 def new_record? _id.nil? end |
#remove ⇒ Object
57 58 59 60 |
# File 'lib/mongodoc/document.rb', line 57 def remove raise UnsupportedOperation.new('Document#remove is not supported for embedded documents') if _root remove_document end |
#remove_document ⇒ Object
62 63 64 65 |
# File 'lib/mongodoc/document.rb', line 62 def remove_document return _root.remove_document if _root _remove end |
#save(validate = true) ⇒ Object
67 68 69 70 71 |
# File 'lib/mongodoc/document.rb', line 67 def save(validate = true) return _root.save(validate) if _root return _save(false) unless validate and not valid? false end |
#save! ⇒ Object
73 74 75 76 77 |
# File 'lib/mongodoc/document.rb', line 73 def save! return _root.save! if _root raise DocumentInvalidError unless valid? _save(true) end |
#to_bson(*args) ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/mongodoc/document.rb', line 79 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 end end |
#to_param ⇒ Object
88 89 90 |
# File 'lib/mongodoc/document.rb', line 88 def to_param _id.to_s end |
#update_attributes(attrs) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/mongodoc/document.rb', line 92 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) end end |
#update_attributes!(attrs) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/mongodoc/document.rb', line 104 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) end end |