Class: ActiveRecordFormatter::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/activerecord/formatter/collector.rb

Constant Summary collapse

SKIP_QUERIES =
["SELECT tablename FROM pg_tables", "select sum(ct) from (select count(*) ct from"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCollector

Returns a new instance of Collector.



8
9
10
11
12
13
14
15
# File 'lib/rspec/activerecord/formatter/collector.rb', line 8

def initialize
  @query_count    = 0
  @objects_count  = 0
  @total_queries  = 0
  @total_objects  = 0

  ActiveSupport::Notifications.subscribe("sql.active_record", method(:record_query))
end

Instance Attribute Details

#objects_countObject (readonly)

Returns the value of attribute objects_count.



5
6
7
# File 'lib/rspec/activerecord/formatter/collector.rb', line 5

def objects_count
  @objects_count
end

#query_countObject (readonly)

Returns the value of attribute query_count.



5
6
7
# File 'lib/rspec/activerecord/formatter/collector.rb', line 5

def query_count
  @query_count
end

#total_objectsObject (readonly)

Returns the value of attribute total_objects.



5
6
7
# File 'lib/rspec/activerecord/formatter/collector.rb', line 5

def total_objects
  @total_objects
end

#total_queriesObject (readonly)

Returns the value of attribute total_queries.



5
6
7
# File 'lib/rspec/activerecord/formatter/collector.rb', line 5

def total_queries
  @total_queries
end

Instance Method Details

#record_query(*_unused, data) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rspec/activerecord/formatter/collector.rb', line 17

def record_query(*_unused, data)
  return if SKIP_QUERIES.any? { |q| data[:sql].index(q) == 0 }

  @query_count    += 1
  @total_queries  += 1

  if query_is_an_insert?(data[:sql])
    @objects_count  += 1
    @total_objects  += 1
  end
end

#reset_exampleObject



29
30
31
32
# File 'lib/rspec/activerecord/formatter/collector.rb', line 29

def reset_example
  @query_count   = 0
  @objects_count = 0
end