Sha256: 2be677dbd147677e72ef4db5d49505aed8a5a0d5d76a6a81f215f80195666457

Contents?: true

Size: 866 Bytes

Versions: 3

Compression:

Stored size: 866 Bytes

Contents

module RSpec::SQLimit
  class Reporter
    attr_reader :matcher

    def initialize(counter)
      @counter = counter
      @count   = counter.count
      @queries = counter.queries
      @matcher = counter.matcher
    end

    def call
      suffix = " among others (see mark ->)" if @matcher
      return "No queries were invoked" if @queries.empty?

      <<-MESSAGE.gsub(/ +\|/, "")
        |The following #{@count} queries were invoked#{suffix}:
        |#{lines.join("\n")}
      MESSAGE
    end

    private

    def lines
      @queries.map.with_index { |*args| line(*args) }
    end

    def line(query, index)
      prefix = (matcher && query[:sql] =~ matcher) ? "->" : "  "
      binds = query[:binds]&.any? ? "; #{query[:binds]} " : ""
      "#{prefix} #{index + 1}) #{query[:sql]}#{binds}" \
      " (#{query[:duration].round(3)} ms)"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rspec-sqlimit-0.0.6 lib/rspec/sqlimit/reporter.rb
rspec-sqlimit-0.0.5 lib/rspec/sqlimit/reporter.rb
rspec-sqlimit-0.0.4 lib/rspec/sqlimit/reporter.rb