Class: RSpec::ActiveRecord::Expectations::Collector
- Inherits:
-
Object
- Object
- RSpec::ActiveRecord::Expectations::Collector
- Defined in:
- lib/rspec/activerecord/expectations/collector.rb
Instance Method Summary collapse
- #calls_by_name(name) ⇒ Object
- #finalize ⇒ Object
-
#initialize ⇒ Collector
constructor
A new instance of Collector.
- #queries_of_type(type) ⇒ Object
- #record_query(*_unused, data) ⇒ Object
- #valid_type?(type) ⇒ Boolean
Constructor Details
#initialize ⇒ Collector
Returns a new instance of Collector.
3 4 5 6 7 8 9 10 11 |
# File 'lib/rspec/activerecord/expectations/collector.rb', line 3 def initialize @inspector = QueryInspector.new @by_name = {} @counts = QueryInspector.valid_query_types.each_with_object({}) do |query_type, hash| hash[query_type] = 0 end @subscription = ActiveSupport::Notifications.subscribe("sql.active_record", method(:record_query)) end |
Instance Method Details
#calls_by_name(name) ⇒ Object
25 26 27 |
# File 'lib/rspec/activerecord/expectations/collector.rb', line 25 def calls_by_name(name) @by_name.fetch(name, 0) end |
#finalize ⇒ Object
13 14 15 |
# File 'lib/rspec/activerecord/expectations/collector.rb', line 13 def finalize ActiveSupport::Notifications.unsubscribe(@subscription) end |
#queries_of_type(type) ⇒ Object
17 18 19 |
# File 'lib/rspec/activerecord/expectations/collector.rb', line 17 def queries_of_type(type) @counts[type] || (raise ArgumentError, "Sorry, #{type} is not a valid kind of query") end |
#record_query(*_unused, data) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rspec/activerecord/expectations/collector.rb', line 29 def record_query(*_unused, data) categories = @inspector.categorize(data) categories.each do |category| @counts[category] += 1 rescue NoMethodError raise "tried to add to to #{category} but it doesn't exist" end @by_name[data[:name]] ||= 0 @by_name[data[:name]] += 1 end |
#valid_type?(type) ⇒ Boolean
21 22 23 |
# File 'lib/rspec/activerecord/expectations/collector.rb', line 21 def valid_type?(type) @counts.include? type end |