Sha256: 25742bfdb70c0defceb91b0ddd3c89af8f9174fada2583f56ab24fe3cf1bea3e

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module Jiminy
  module Recording
    class NPlusOne
      attr_reader :file, :location

      LOCATION_REGEXP = %r{^
        (?<file>[\w_\-/.]+\.(?:erb|rb|jbuilder)):
        (?<line>\d+):in\s`(?:block\sin\s)?
        (?<method>[^']+)'?}x.freeze

      EXAMPLES_COUNT = 3

      def initialize(location:, queries: [])
        @location = location.to_s.strip
        @queries = queries
        match_result = location.match(LOCATION_REGEXP)

        @file = match_result[:file]
        @line = match_result[:line]
        @method_name = match_result[:method]

        freeze
      end

      def ==(other)
        location == other.location
      end

      def to_h
        {
          location => attributes
        }
      end

      private

        attr_reader :queries, :line, :method_name

        def attributes
          {
            "file" => file,
            "line" => line,
            "method" => method_name,
            "examples" => queries.take(EXAMPLES_COUNT)
          }
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jiminy-0.4.0 lib/jiminy/recording/n_plus_one.rb