class RSpec::SQLimit::Counter
Attributes
matcher[R]
queries[R]
Public Class Methods
[](*args)
click to toggle source
# File lib/rspec/sqlimit/counter.rb, line 5 def self.[](*args) new(*args).tap(&:call) end
new(matcher, block)
click to toggle source
# File lib/rspec/sqlimit/counter.rb, line 9 def initialize(matcher, block) @queries = [] @matcher = matcher @block = block @mutex = Mutex.new end
Public Instance Methods
call()
click to toggle source
# File lib/rspec/sqlimit/counter.rb, line 16 def call @mutex.synchronize do @queries = [] ActiveSupport::Notifications.subscribed callback, "sql.active_record" do @block.call end end end
count()
click to toggle source
# File lib/rspec/sqlimit/counter.rb, line 25 def count matcher ? queries.count { |query| query[:sql] =~ matcher } : queries.count end
Private Instance Methods
cached_query?(values)
click to toggle source
# File lib/rspec/sqlimit/counter.rb, line 50 def cached_query?(values) values[:type_casted_binds].respond_to?(:call) end
callback()
click to toggle source
# File lib/rspec/sqlimit/counter.rb, line 31 def callback @callback ||= lambda do |_name, start, finish, _message_id, values| return if %w[CACHE SCHEMA].include? values[:name] return if cached_query?(values) queries << { sql: values[:sql], duration: (finish - start) * 1_000, binds: values[:type_casted_binds] || type_cast(values[:binds]) } end end
type_cast(binds)
click to toggle source
# File lib/rspec/sqlimit/counter.rb, line 44 def type_cast(binds) binds.map do |column, value| ActiveRecord::Base.connection.type_cast(value, column) end end