Module: Mongoid::Persistence::Atomic

Defined in:
lib/patches/atomic.rb

Instance Method Summary collapse

Instance Method Details

#add_to_set_with_mongoid4(*args) ⇒ Object

push_all is deprecated so not supported



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/patches/atomic.rb', line 121

def add_to_set_with_mongoid4(*args)
  if args.length == 1 && args.first.is_a?(Hash)
    adds = args.first
    prepare_atomic_operation do |ops|
      process_atomic_operations(adds) do |field, value|
        existing = send(field) || (attributes[field] ||= [])
        values = [ value ].flatten(1)
        values.each do |val|
          existing.push(val) unless existing.include?(val)
        end
        ops[atomic_attribute_name(field)] = { "$each" => values }
      end
      { "$addToSet" => ops }
    end
  else
    add_to_set_without_mongoid4(*args)
  end
end

#bit_with_mongoid4(*args) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/patches/atomic.rb', line 141

def bit_with_mongoid4(*args)
  if args.length == 1 && args.first.is_a?(Hash)
    operations = args.first
    prepare_atomic_operation do |ops|
      process_atomic_operations(operations) do |field, values|
        value = attributes[field]
        values.each do |op, val|
          value = value & val if op.to_s == "and"
          value = value | val if op.to_s == "or"
        end
        attributes[field] = value
        ops[atomic_attribute_name(field)] = values
      end
      { "$bit" => ops }
    end
  else
    bit_without_mongoid4(*args)
  end
end

#inc_with_mongoid4(*args) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/patches/atomic.rb', line 162

def inc_with_mongoid4(*args)
  if args.length == 1 && args.first.is_a?(Hash)
    increments = args.first
    prepare_atomic_operation do |ops|
      process_atomic_operations(increments) do |field, value|
        increment = value.__to_inc__
        current = attributes[field]
        attributes[field] = (current || 0) + increment
        ops[atomic_attribute_name(field)] = increment
      end
      { "$inc" => ops }
    end
  else
    inc_without_mongoid4(*args)
  end
end

#pop_with_mongoid4(*args) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/patches/atomic.rb', line 180

def pop_with_mongoid4(*args)
  if args.length == 1 && args.first.is_a?(Hash)
    pops = args.first
    prepare_atomic_operation do |ops|
      process_atomic_operations(pops) do |field, value|
        values = send(field)
        value > 0 ? values.pop : values.shift
        ops[atomic_attribute_name(field)] = value
      end
      { "$pop" => ops }
    end
  else
    pop_without_mongoid4(*args)
  end
end

#pull_all_with_mongoid4(*args) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/patches/atomic.rb', line 213

def pull_all_with_mongoid4(*args)
  if args.length == 1 && args.first.is_a?(Hash)
    pulls = args.first
    prepare_atomic_operation do |ops|
      process_atomic_operations(pulls) do |field, value|
        existing = send(field) || []
        value.each{ |val| existing.delete(val) }
        ops[atomic_attribute_name(field)] = value
      end
      { "$pullAll" => ops }
    end
  else
    pull_all_without_mongoid4(*args)
  end
end

#pull_with_mongoid4(*args) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/patches/atomic.rb', line 197

def pull_with_mongoid4(*args)
  if args.length == 1 && args.first.is_a?(Hash)
    pulls = args.first
    prepare_atomic_operation do |ops|
      process_atomic_operations(pulls) do |field, value|
        (send(field) || []).delete(value)
        ops[atomic_attribute_name(field)] = value
      end
      { "$pull" => ops }
    end
  else
    pull_without_mongoid4(*args)
  end
end

#push_with_mongoid4(*args) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/patches/atomic.rb', line 230

def push_with_mongoid4(*args)
  if args.length == 1 && args.first.is_a?(Hash)
    pushes = args.first
    prepare_atomic_operation do |ops|
      process_atomic_operations(pushes) do |field, value|
        existing = send(field) || (attributes[field] ||= [])
        values = [ value ].flatten(1)
        values.each{ |val| existing.push(val) }
        ops[atomic_attribute_name(field)] = { "$each" => values }
      end
      { "$push" => ops }
    end
  else
    push_without_mongoid4(*args)
  end
end

#rename_with_mongoid4(*args) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/patches/atomic.rb', line 248

def rename_with_mongoid4(*args)
  if args.length == 1 && args.first.is_a?(Hash)
    renames = args.first
    prepare_atomic_operation do |ops|
      process_atomic_operations(renames) do |old_field, new_field|
        new_name = new_field.to_s
        attributes[new_name] = attributes.delete(old_field)
        ops[atomic_attribute_name(old_field)] = atomic_attribute_name(new_name)
      end
      { "$rename" => ops }
    end
  else
    rename_without_mongoid4(*args)
  end
end

#set_with_mongoid4(*args) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/patches/atomic.rb', line 265

def set_with_mongoid4(*args)
  if args.length == 1 && args.first.is_a?(Hash)
    setters = args.first
    prepare_atomic_operation do |ops|
      process_atomic_operations(setters) do |field, value|
        process_attribute(field.to_s, value)
        ops[atomic_attribute_name(field)] = attributes[field]
      end
      { "$set" => ops }
    end
  else
    set_without_mongoid4(*args)
  end
end