<commit_before>module Calabash
  # A QueryResult represents the returned value of a query.
  # It is an immutable collection of matches or the result of applied methods.
  # It will, unlike `Array`, raise an IndexError instead of returning nil if
  # an entry outside bounds is accessed.
  class QueryResult < Array
    # @!visibility private
    def self.create(result, query)
      query_result = QueryResult.send(:new, query)
      query_result.send(:initialize_copy, result)
      recursive_freeze(query_result)
      query_result
    end

    private_class_method :new

    # @!visibility private
    attr_reader :query

    # @!visibility private
    def initialize(query)
      @query = Query.new(query)
    end

    # @!visibility private
    def first(*several_variants)
      ensure_in_bounds(0)

      super(*several_variants)
    end

    # @!visibility private
    def last(*several_variants)
      ensure_in_bounds(-1)

      super(*several_variants)
    end

    # @!visibility private
    def [](index)
      ensure_in_bounds(index)

      super(index)
    end

    # @!visibility private
    def at(index)
      ensure_in_bounds(index)

      super(index)
    end

    # @!visibility private
    def fetch(*several_variants)
      unless block_given? || several_variants.length > 1
        ensure_in_bounds(several_variants.first)
      end

      super(*several_variants)
    end

    # @!visibility private
    def ensure_in_bounds(index)
      if empty?
        raise IndexError, "Query result is empty"
      end

      if index > 0 && index >= length
        raise IndexError, "Index out of bounds [index: #{index}, length: #{length}]"
      end

      if index < 0 && -index > length
        raise IndexError, "Index out of bounds [index: #{index}, length: #{length}]"
      end
    end

    # @!visibility private
    def self.recursive_freeze(object)<commit_msg>QueryResult: If key isin't numberic, it doesn't have to be in bounds<commit_after>module Calabash
  # A QueryResult represents the returned value of a query.
  # It is an immutable collection of matches or the result of applied methods.
  # It will, unlike `Array`, raise an IndexError instead of returning nil if
  # an entry outside bounds is accessed.
  class QueryResult < Array
    # @!visibility private
    def self.create(result, query)
      query_result = QueryResult.send(:new, query)
      query_result.send(:initialize_copy, result)
      recursive_freeze(query_result)
      query_result
    end

    private_class_method :new

    # @!visibility private
    attr_reader :query

    # @!visibility private
    def initialize(query)
      @query = Query.new(query)
    end

    # @!visibility private
    def first(*several_variants)
      ensure_in_bounds(0)

      super(*several_variants)
    end

    # @!visibility private
    def last(*several_variants)
      ensure_in_bounds(-1)

      super(*several_variants)
    end

    # @!visibility private
    def [](index)
      ensure_in_bounds(index)

      super(index)
    end

    # @!visibility private
    def at(index)
      ensure_in_bounds(index)

      super(index)
    end

    # @!visibility private
    def fetch(*several_variants)
      unless block_given? || several_variants.length > 1
        ensure_in_bounds(several_variants.first)
      end

      super(*several_variants)
    end

    # @!visibility private
    def ensure_in_bounds(index)
      unless index.is_a?(Numeric)
        return true
      end

      if empty?
        raise IndexError, "Query result is empty"
      end

      if index > 0 && index >= length
        raise IndexError, "Index out of bounds [index: #{index}, length: #{length}]"
      end

      if index < 0 && -index > length
        raise IndexError, "Index out of bounds [index: #{index}, length: #{length}]"
      end
    end

    # @!visibility private
    def self.recursive_freeze(object)
{'fraction_non_alphanumeric': 0.08577298407806883, 'fraction_numerical': 0.0025680534155110425, 'mean_word_length': 2.720152817574021, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5421691', 'n_tokens_mistral': 1211, 'n_tokens_neox': 1115, 'n_words': 337}