class QueryMatchers::QueryCounter

Constants

OPERATIONS
RAILS5_INFORMATION_SCHEMA_REGEX

Public Class Methods

new() click to toggle source
# File lib/query_matchers/query_counter.rb, line 6
def initialize
  @events = []
end

Public Instance Methods

execute!(target) click to toggle source
# File lib/query_matchers/query_counter.rb, line 10
def execute!(target)
  ActiveSupport::Notifications.subscribed(subscriber, 'sql.active_record', &target)
end
queries() click to toggle source
# File lib/query_matchers/query_counter.rb, line 18
def queries
  @events.map {|event| event.payload[:sql] }
end
query_count() click to toggle source
# File lib/query_matchers/query_counter.rb, line 14
def query_count
  @events.size
end

Private Instance Methods

count_query?(sql) click to toggle source
# File lib/query_matchers/query_counter.rb, line 31
def count_query?(sql)
  OPERATIONS.any? {|op| sql.lstrip.start_with?(op) } && !ignore_query?(sql)
end
ignore_query?(sql) click to toggle source
# File lib/query_matchers/query_counter.rb, line 35
def ignore_query?(sql)
  sql.match?(RAILS5_INFORMATION_SCHEMA_REGEX)
end
subscriber() click to toggle source
# File lib/query_matchers/query_counter.rb, line 24
def subscriber
  lambda do |*args|
    event = ActiveSupport::Notifications::Event.new(*args)
    @events << event if count_query?(event.payload[:sql])
  end
end