Class: Mongoid::QueryCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/support/query_counter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQueryCounter

Returns a new instance of QueryCounter.



6
7
8
# File 'lib/mongoid/support/query_counter.rb', line 6

def initialize
  @events = []
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



4
5
6
# File 'lib/mongoid/support/query_counter.rb', line 4

def events
  @events
end

Instance Method Details

#inspectObject



19
20
21
# File 'lib/mongoid/support/query_counter.rb', line 19

def inspect
  @events.map { |e| e.payload[:ops].map(&:log_inspect) }.join("\n")
end

#instrumentObject



10
11
12
13
14
15
16
17
# File 'lib/mongoid/support/query_counter.rb', line 10

def instrument
  subscriber = ActiveSupport::Notifications.subscribe('query.moped') do |*args|
    @events << ActiveSupport::Notifications::Event.new(*args)
  end
  yield
ensure
  ActiveSupport::Notifications.unsubscribe(subscriber)
end