Class: ActiveRecordQueryCount::Compare::Comparator

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_query_count/compare/comparator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeComparator

Returns a new instance of Comparator.



6
7
8
9
# File 'lib/active_record_query_count/compare/comparator.rb', line 6

def initialize
  @results = {}
  @scripts_loaded = 0
end

Instance Attribute Details

#resultsObject

Returns the value of attribute results.



4
5
6
# File 'lib/active_record_query_count/compare/comparator.rb', line 4

def results
  @results
end

#scripts_loadedObject

Returns the value of attribute scripts_loaded.



4
5
6
# File 'lib/active_record_query_count/compare/comparator.rb', line 4

def scripts_loaded
  @scripts_loaded
end

Instance Method Details

#code(name) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/active_record_query_count/compare/comparator.rb', line 11

def code(name)
  @scripts_loaded += 1

  ActiveRecordQueryCount.start_with_block(printer: :none) do
    yield
    @results[name] = ActiveRecordQueryCount.tracker.active_record_query_tracker.dup
  end
end

#compare!Object



20
21
22
23
24
25
# File 'lib/active_record_query_count/compare/comparator.rb', line 20

def compare!
  raise 'Exactly two code blocks are required' if @scripts_loaded != 2

  ActiveRecordQueryCount::Printer::HtmlCompare.new(data_1: results.slice(results.keys[0]),
                                                   data_2: results.slice(results.keys[1])).print
end