Module: Mongoo::Persistence

Included in:
Base
Defined in:
lib/mongoo/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


15
16
17
# File 'lib/mongoo/persistence.rb', line 15

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#collectionObject


125
126
127
# File 'lib/mongoo/persistence.rb', line 125

def collection
  self.class.collection
end

#destroyed?Boolean

Returns:

  • (Boolean)

197
198
199
# File 'lib/mongoo/persistence.rb', line 197

def destroyed?
  @destroyed != nil
end

#insert(opts = {}) ⇒ Object


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/mongoo/persistence.rb', line 129

def insert(opts={})
  ret = _run_insert_callbacks do
    if persisted?
      raise AlreadyInsertedError, "document has already been inserted"
    end
    unless valid?
      if opts[:safe] == true
        raise Mongoo::NotValidError, "document contains errors"
      else
        return false
      end
    end
    ret = self.collection.insert(mongohash.deep_clone, opts)
    unless ret.is_a?(BSON::ObjectId)
      raise InsertError, "not an object: #{ret.inspect}"
    end
    set("_id", ret)
    @persisted = true
    set_persisted_mongohash(mongohash.deep_clone)
    ret
  end
  Mongoo::IdentityMap.write(self) if Mongoo::IdentityMap.on?
  ret
end

#insert!(opts = {}) ⇒ Object


154
155
156
# File 'lib/mongoo/persistence.rb', line 154

def insert!(opts={})
  insert(opts.merge(:safe => true))
end

#new_record?Boolean

Returns:

  • (Boolean)

201
202
203
# File 'lib/mongoo/persistence.rb', line 201

def new_record?
  !persisted?
end

#persisted?Boolean

Returns:

  • (Boolean)

120
121
122
123
# File 'lib/mongoo/persistence.rb', line 120

def persisted?
  @persisted == true
  #!get("_id").nil?
end

#reloadObject


225
226
227
228
229
230
# File 'lib/mongoo/persistence.rb', line 225

def reload
  init_from_hash(collection.find_one(get("_id")))
  @persisted = true
  set_persisted_mongohash(mongohash.deep_clone)
  true
end

#remove(opts = {}) ⇒ Object


205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/mongoo/persistence.rb', line 205

def remove(opts={})
  _run_remove_callbacks do
    unless persisted?
      raise NotInsertedError, "document must be inserted before it can be removed"
    end
    ret = self.collection.remove({"_id" => get("_id")}, opts)
    if !ret.is_a?(Hash) || (ret["err"] == nil && ret["n"] == 1)
      @destroyed = true
      @persisted = false
      true
    else
      raise RemoveError, ret.inspect
    end
  end
end

#remove!(opts = {}) ⇒ Object


221
222
223
# File 'lib/mongoo/persistence.rb', line 221

def remove!(opts={})
  remove(opts.merge(:safe => true))
end

#to_keyObject


112
113
114
# File 'lib/mongoo/persistence.rb', line 112

def to_key
  get("_id")
end

#to_modelObject


116
117
118
# File 'lib/mongoo/persistence.rb', line 116

def to_model
  self
end

#to_paramObject

ClassMethods


108
109
110
# File 'lib/mongoo/persistence.rb', line 108

def to_param
  persisted? ? get("_id").to_s : nil
end

#update(opts = {}) ⇒ Object


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/mongoo/persistence.rb', line 158

def update(opts={})
  _run_update_callbacks do
    unless persisted?
      raise NotInsertedError, "document must be inserted before being updated"
    end
    unless valid?
      if opts[:safe] == true
        raise Mongoo::NotValidError, "document contains errors"
      else
        return false
      end
    end
    opts[:only_if_current] = true unless opts.has_key?(:only_if_current)
    opts[:safe] = true if !opts.has_key?(:safe) && opts[:only_if_current] == true
    update_hash = build_update_hash(self.changelog)
    return true if update_hash.empty?
    update_query_hash = build_update_query_hash(persisted_mongohash.to_key_value, self.changelog)
    if Mongoo.verbose_debug
      puts "\n* update_query_hash: #{update_query_hash.merge({"_id" => get("_id")}).inspect}\n  update_hash: #{update_hash.inspect}\n  opts: #{opts.inspect}\n"
    end
    ret = self.collection.update(update_query_hash.merge({"_id" => get("_id")}), update_hash, opts)
    if !ret.is_a?(Hash) || (ret["updatedExisting"] && ret["n"] == 1)
      set_persisted_mongohash(mongohash.deep_clone)
      @persisted = true
      true
    else
      if opts[:only_if_current]
        raise StaleUpdateError, ret.inspect
      else
        raise UpdateError, ret.inspect
      end
    end
  end
end

#update!(opts = {}) ⇒ Object


193
194
195
# File 'lib/mongoo/persistence.rb', line 193

def update!(opts={})
  update(opts.merge(:safe => true))
end