Module: MongoDoc::Document

Defined in:
lib/mongo_doc/document.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

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

#_collectionObject



45
46
47
# File 'lib/mongo_doc/document.rb', line 45

def _collection
  _root and _root._collection or self.class.collection
end

#destroyed?Boolean

Returns:



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

Returns:



62
63
64
# File 'lib/mongo_doc/document.rb', line 62

def new_record?
  _id.nil?
end

#persisted?Boolean

Returns:



66
67
68
# File 'lib/mongo_doc/document.rb', line 66

def persisted?
  _id.present?
end

#removeObject



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_documentObject



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
# 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
  end
end

#to_keyObject



101
102
103
# File 'lib/mongo_doc/document.rb', line 101

def to_key
  persisted? ? [_id] : nil
end

#to_modelObject



105
106
107
# File 'lib/mongo_doc/document.rb', line 105

def to_model
  self
end

#to_paramObject



109
110
111
# File 'lib/mongo_doc/document.rb', line 109

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.



128
129
130
131
132
133
134
135
136
# File 'lib/mongo_doc/document.rb', line 128

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



113
114
115
116
117
118
# File 'lib/mongo_doc/document.rb', line 113

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



120
121
122
123
124
125
# File 'lib/mongo_doc/document.rb', line 120

def update_attributes!(attrs)
  self.attributes = attrs
  return save! if new_record?
  raise DocumentInvalidError unless valid?
  _update_attributes(converted_attributes(attrs), true)
end