Class: Moped::PromiscuousQueryWrapper

Inherits:
Query
  • Object
show all
Defined in:
lib/promiscuous/publisher/operation/mongoid.rb

Defined Under Namespace

Modules: PromiscuousHelpers Classes: PromiscuousReadOperation, PromiscuousWriteOperation

Instance Method Summary collapse

Instance Method Details

#count(*args) ⇒ Object

Moped::Query



234
235
236
# File 'lib/promiscuous/publisher/operation/mongoid.rb', line 234

def count(*args)
  promiscuous_read_operation(:operation_ext => :count).execute { super }.to_i
end

#distinct(key) ⇒ Object



238
239
240
# File 'lib/promiscuous/publisher/operation/mongoid.rb', line 238

def distinct(key)
  promiscuous_read_operation(:operation_ext => :distinct).execute { super }
end

#eachObject Also known as: cursor



242
243
244
245
246
247
248
249
# File 'lib/promiscuous/publisher/operation/mongoid.rb', line 242

def each
  # The TLS is used to pass arguments to the Cursor so we don't hijack more than
  # necessary.
  old_moped_query, Thread.current[:moped_query] = Thread.current[:moped_query], self
  super
ensure
  Thread.current[:moped_query] = old_moped_query
end

#firstObject Also known as: one



252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/promiscuous/publisher/operation/mongoid.rb', line 252

def first
  # FIXME If the the user is using something like .only(), we need to make
  # sure that we add the id, otherwise we are screwed.
  op = promiscuous_read_operation

  op.execute do |query|
    query.non_instrumented { super }
    query.instrumented do
      super.tap do |doc|
        op.instances = doc ? [Mongoid::Factory.from_db(op.model, doc)] : []
      end
    end
  end
end

#modify(change, options = {}) ⇒ Object



286
287
288
289
290
291
292
293
294
# File 'lib/promiscuous/publisher/operation/mongoid.rb', line 286

def modify(change, options={})
  promiscuous_write_operation(:update, :change => change).execute do |query|
    query.non_instrumented { super }
    query.instrumented do |op|
      raise "You can only use find_and_modify() with :new => true" if !options[:new]
      super.tap { |raw_instance| op.instance = Mongoid::Factory.from_db(op.model, raw_instance) }
    end
  end
end

#promiscuous_read_operation(options = {}) ⇒ Object



219
220
221
# File 'lib/promiscuous/publisher/operation/mongoid.rb', line 219

def promiscuous_read_operation(options={})
  PromiscuousReadOperation.new(options.merge(:query => self))
end

#promiscuous_write_operation(operation, options = {}) ⇒ Object



223
224
225
# File 'lib/promiscuous/publisher/operation/mongoid.rb', line 223

def promiscuous_write_operation(operation, options={})
  PromiscuousWriteOperation.new(options.merge(:query => self, :operation => operation))
end

#removeObject



296
297
298
# File 'lib/promiscuous/publisher/operation/mongoid.rb', line 296

def remove
  promiscuous_write_operation(:destroy).execute { super }
end

#remove_allObject



300
301
302
303
# File 'lib/promiscuous/publisher/operation/mongoid.rb', line 300

def remove_all
  raise "Instead of doing a multi delete, delete each document separatly.\n" +
        "Declare your has_many relationships with :dependent => :destroy instead of :delete"
end

#selector=(value) ⇒ Object



227
228
229
230
# File 'lib/promiscuous/publisher/operation/mongoid.rb', line 227

def selector=(value)
  @selector = value
  @operation.selector = value
end

#update(change, flags = nil) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/promiscuous/publisher/operation/mongoid.rb', line 268

def update(change, flags=nil)
  update_op = promiscuous_write_operation(:update, :change => change)

  if flags && update_op.should_instrument_query?
    raise "You cannot do a multi update. Instead, update each document separately." if flags.include?(:multi)
    raise "No upsert support yet" if flags.include?(:upsert)
  end

  update_op.execute do |query|
    query.non_instrumented { super }
    query.instrumented do |op|
      raw_instance = without_promiscuous { modify(change, :new => true) }
      op.instance = Mongoid::Factory.from_db(op.model, raw_instance)
      {'updatedExisting' => true, 'n' => 1, 'err' => nil, 'ok' => 1.0}
    end
  end
end