Method: Mongo::ObjectSerializer#insert

Defined in:
lib/mongo_db/object/object_serializer.rb

#insert(opts, collection) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mongo_db/object/object_serializer.rb', line 26

def insert opts, collection
  opts, validate, callbacks = parse_object_options opts

  # before callbacks
  return false if callbacks and !run_callbacks(objects, [:before, :validate], [:before, :save], [:before, :create])

  # validation
  return false if validate  and !valid?

  # saving document
  doc = to_document
  collection.insert_without_object doc, opts
  id = doc[:_id] || doc['_id'] || raise("internal error: no id after document insertion (#{doc})!")
  object.instance_variable_set :@_id, id
  update_internal_state!

  # after callbacks
  run_callbacks(objects, [:after, :create], [:after, :save], [:after, :validate]) if callbacks

  true
end