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

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



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

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



262
263
264
# File 'lib/promiscuous/publisher/operation/mongoid.rb', line 262

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

#remove_allObject



266
267
268
269
# File 'lib/promiscuous/publisher/operation/mongoid.rb', line 266

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

Moped::Query



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/promiscuous/publisher/operation/mongoid.rb', line 234

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