Class: Mongo::ObjectSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo_db/object/object_serializer.rb

Constant Summary collapse

SIMPLE_TYPES =
[
  Fixnum, Float,
  TrueClass, FalseClass,
  String, Symbol,
  Array, Hash, Set,
  Data, DateTime,
  NilClass, Time,
  BSON::ObjectId
].to_set

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ ObjectSerializer

Returns a new instance of ObjectSerializer.



14
15
16
# File 'lib/mongo_db/object/object_serializer.rb', line 14

def initialize object
  @object = object
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



12
13
14
# File 'lib/mongo_db/object/object_serializer.rb', line 12

def object
  @object
end

Class Method Details

.build(doc) ⇒ Object



229
230
231
232
233
234
# File 'lib/mongo_db/object/object_serializer.rb', line 229

def build doc
  obj = _build doc
  serializer = Mongo::ObjectSerializer.new obj
  serializer.update_internal_state!
  obj
end

Instance Method Details

#_idObject



119
120
121
# File 'lib/mongo_db/object/object_serializer.rb', line 119

def _id
  object.instance_variable_get(:@_id)
end

#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

#objectsObject



141
142
143
144
145
146
147
# File 'lib/mongo_db/object/object_serializer.rb', line 141

def objects
  @objects_cache ||= begin
    objects = []
    _each_object(object){|obj| objects << obj}
    objects
  end
end

#remove(opts, collection) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/mongo_db/object/object_serializer.rb', line 91

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

  # before callbacks
  if callbacks
    # we need to run :destroy callbacks also on detached embedded objects.
    all_objects = (objects + original_objects).uniq{|o| o.object_id}
    return false unless run_callbacks(all_objects, [:before, :validate], [:before, :destroy])
  end

  # validation
  return false if validate  and !valid?

  # saving document
  id = _id || "can't destroy object without _id (#{arg})!"
  collection.remove_without_object({_id: id}, opts)
  update_internal_state!

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

  true
end

#run_callbacks(objects, *callbacks) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/mongo_db/object/object_serializer.rb', line 130

def run_callbacks objects, *callbacks
  callbacks.each do |type, method_name|
    objects.each do |obj|
      if obj.respond_to? :_run_callbacks
        return false if obj._run_callbacks(type, method_name) == false
      end
    end
  end
  true
end

#save(opts, collection) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/mongo_db/object/object_serializer.rb', line 18

def save opts, collection
  if _id
    self.update opts.merge(upsert: true), collection
  else
    self.insert opts, collection
  end
end

#to_documentObject



115
116
117
# File 'lib/mongo_db/object/object_serializer.rb', line 115

def to_document
  _to_document object
end

#update(opts, collection) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mongo_db/object/object_serializer.rb', line 48

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

  # before callbacks.
  # we need to sort out embedded objects into created, updated and destroyed
  created_objects, updated_objects, destroyed_objects = [], [], []
  if callbacks
    original_ids = original_objects.collect{|obj| obj.object_id}.to_set
    objects.each do |obj|
      (original_ids.include?(obj.object_id) ? updated_objects : created_objects) << obj
    end

    objects_ids = objects.collect{|obj| obj.object_id}.to_set
    destroyed_objects = original_objects.select{|obj| !objects_ids.include?(obj.object_id)}

    all_successfull = [
      run_callbacks(created_objects,   [:before, :validate], [:before, :save],   [:before, :create]),
      run_callbacks(updated_objects,   [:before, :validate], [:before, :save],   [:before, :update]),
      run_callbacks(destroyed_objects, [:before, :validate], [:before, :destroy])
    ].reduce(:&)

    return false unless all_successfull
  end

  # validation
  return false if validate  and !valid?

  # saving document
  doc = to_document
  id = _id || raise("can't update document without id (#{doc})!")
  collection.update_without_object({_id: id}, doc, opts)
  update_internal_state!

  # after callbacks
  if callbacks
    run_callbacks(created_objects,   [:after, :create],  [:after, :save],    [:after, :validate])
    run_callbacks(updated_objects,   [:after, :update],  [:after, :save],    [:after, :validate])
    run_callbacks(destroyed_objects, [:after, :destroy], [:after, :validate])
  end

  true
end

#update_internal_state!Object



149
150
151
# File 'lib/mongo_db/object/object_serializer.rb', line 149

def update_internal_state!
  self.original_objects = objects if Mongo.defaults[:callbacks]
end

#valid?Boolean

Returns:



123
124
125
126
127
128
# File 'lib/mongo_db/object/object_serializer.rb', line 123

def valid?
  objects.each do |obj|
    return false if obj.respond_to?(:_valid?) and !obj._valid?
  end
  true
end