Class: RSpec::SQLimit::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/sqlimit/counter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matcher, block) ⇒ Counter

Returns a new instance of Counter.



9
10
11
12
13
14
# File 'lib/rspec/sqlimit/counter.rb', line 9

def initialize(matcher, block)
  @queries = []
  @matcher = matcher
  @block   = block
  @mutex   = Mutex.new
end

Instance Attribute Details

#matcherObject (readonly)

Returns the value of attribute matcher.



3
4
5
# File 'lib/rspec/sqlimit/counter.rb', line 3

def matcher
  @matcher
end

#queriesObject (readonly)

Returns the value of attribute queries.



3
4
5
# File 'lib/rspec/sqlimit/counter.rb', line 3

def queries
  @queries
end

Class Method Details

.[](*args) ⇒ Object



5
6
7
# File 'lib/rspec/sqlimit/counter.rb', line 5

def self.[](*args)
  new(*args).tap(&:call)
end

Instance Method Details

#callObject



16
17
18
19
20
21
22
23
# 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

#countObject



25
26
27
# File 'lib/rspec/sqlimit/counter.rb', line 25

def count
  matcher ? queries.count { |query| query[:sql] =~ matcher } : queries.count
end