Class: DBQueryMatchers::QueryCounter
- Inherits:
-
Object
- Object
- DBQueryMatchers::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(options = {}) ⇒ QueryCounter
constructor
A new instance of QueryCounter.
-
#to_proc ⇒ Proc
Turns a QueryCounter instance into a lambda.
Constructor Details
#initialize(options = {}) ⇒ QueryCounter
Returns a new instance of QueryCounter.
19 20 21 22 23 24 |
# File 'lib/db_query_matchers/query_counter.rb', line 19 def initialize( = {}) @matches = [:matches] @database_role = [:database_role] @count = 0 @log = [] end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
17 18 19 |
# File 'lib/db_query_matchers/query_counter.rb', line 17 def count @count end |
#log ⇒ Object (readonly)
Returns the value of attribute log.
17 18 19 |
# File 'lib/db_query_matchers/query_counter.rb', line 17 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.
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/db_query_matchers/query_counter.rb', line 42 def callback(_name, _start, _finish, , payload) return if @database_role && (ActiveRecord::Base.current_role != @database_role) return if @matches && !any_match?(@matches, payload[:sql]) return if any_match?(DBQueryMatchers.configuration.ignores, payload[:sql]) return if DBQueryMatchers.configuration.ignore_cached && payload[:cached] return if DBQueryMatchers.configuration.schemaless && payload[:name] == "SCHEMA" count_query log_query(payload[:sql]) DBQueryMatchers.configuration.on_query_counted.call(payload) end |
#to_proc ⇒ Proc
Turns a QueryCounter instance into a lambda. Designed to be used when subscribing to events through the ActiveSupport::Notifications module.
30 31 32 |
# File 'lib/db_query_matchers/query_counter.rb', line 30 def to_proc lambda(&method(:callback)) end |