Sha256: 801bfaaabf1f4e750fc9619504657b3ede25fa7450cd1275febf223e6719230d

Contents?: true

Size: 1005 Bytes

Versions: 4

Compression:

Stored size: 1005 Bytes

Contents

# frozen_string_literal: true

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

      LOCATION_REGEXP = /^(?<file>[\w_\-\/.]+\.e?rb):(?<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

4 entries across 4 versions & 1 rubygems

Version Path
jiminy-0.3.0 lib/jiminy/recording/n_plus_one.rb
jiminy-0.2.0 lib/jiminy/recording/n_plus_one.rb
jiminy-0.1.1 lib/jiminy/recording/n_plus_one.rb
jiminy-0.1.0 lib/jiminy/recording/n_plus_one.rb