Class: QueryCounter
- Inherits:
-
Object
- Object
- QueryCounter
- Defined in:
- lib/db_query_matchers/query_counter.rb
Overview
Counter to keep track of the number of queries caused by running a piece of code. Closely tied to the ‘:make_database_queries` matcher, this class is designed to be a consumer of `sql.active_record` events.
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#log ⇒ Object
readonly
Returns the value of attribute log.
Instance Method Summary collapse
-
#callback(name, start, finish, message_id, payload) ⇒ Object
Method called from the ActiveSupport::Notifications module (through the lambda created by ‘to_proc`) when an SQL query is made.
-
#initialize ⇒ QueryCounter
constructor
A new instance of QueryCounter.
-
#to_proc ⇒ Proc
Turns a QueryCounter instance into a lambda.
Constructor Details
#initialize ⇒ QueryCounter
Returns a new instance of QueryCounter.
18 19 20 21 |
# File 'lib/db_query_matchers/query_counter.rb', line 18 def initialize @count = 0 @log = [] end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
16 17 18 |
# File 'lib/db_query_matchers/query_counter.rb', line 16 def count @count end |
#log ⇒ Object (readonly)
Returns the value of attribute log.
16 17 18 |
# File 'lib/db_query_matchers/query_counter.rb', line 16 def log @log end |
Instance Method Details
#callback(name, start, finish, message_id, payload) ⇒ Object
Method called from the ActiveSupport::Notifications module (through the lambda created by ‘to_proc`) when an SQL query is made.
39 40 41 42 |
# File 'lib/db_query_matchers/query_counter.rb', line 39 def callback(name, start, finish, , payload) @count += 1 @log << payload[:sql] end |
#to_proc ⇒ Proc
Turns a QueryCounter instance into a lambda. Designed to be used when subscribing to events through the ActiveSupport::Notifications module.
27 28 29 |
# File 'lib/db_query_matchers/query_counter.rb', line 27 def to_proc lambda(&method(:callback)) end |