<commit_before># frozen_string_literal: true

module ActiveTsv
  class Relation
    include Enumerable

    def initialize(table, conditions)
      @table = table
      @conditions = conditions
    end

    def where(condition = nil)
      if condition
        self.class.new(@table, @conditions << Condition.new(:==, condition))
      else
        WhereChain.new(@table, @conditions)
      end
    end

    def exist?
      !first.nil?
    end

    def first
      keys = @table.keys
      key_to_value_index = keys.each_with_index.map { |k, index| [k, index] }.to_h
      value = @table.open { |csv|
        csv.gets
        csv.find do |value|
          @conditions.all? do |cond|
            cond.values.all? do |k, v|
              value[key_to_value_index[k]].__send__(cond.method_name, v.to_s)
            end
          end
        end
      }
      if value
        @table.new(@table.keys.zip(value).to_h)
      else
        nil
      end
    end

    def last
      to_a.last
    end

    def each
      return to_enum(:each) unless block_given?

      keys = @table.keys
      key_to_value_index = keys.each_with_index.map { |k, index| [k, index] }.to_h
      values = @table.open { |csv|
        csv.gets
        csv.select do |value|
          @conditions.all? do |cond|
            cond.values.all? do |k, v|
              value[key_to_value_index[k]].__send__(cond.method_name, v.to_s)
            end
          end
        end
      }
      values.map do |value|
        yield @table.new(@table.keys.zip(value).to_h)
      end
    end
  end
end<commit_msg>Should use each<commit_after># frozen_string_literal: true

module ActiveTsv
  class Relation
    include Enumerable

    def initialize(table, conditions)
      @table = table
      @conditions = conditions
    end

    def where(condition = nil)
      if condition
        self.class.new(@table, @conditions << Condition.new(:==, condition))
      else
        WhereChain.new(@table, @conditions)
      end
    end

    def exist?
      !first.nil?
    end

    def first
      keys = @table.keys
      key_to_value_index = keys.each_with_index.map { |k, index| [k, index] }.to_h
      value = @table.open { |csv|
        csv.gets
        csv.find do |value|
          @conditions.all? do |cond|
            cond.values.all? do |k, v|
              value[key_to_value_index[k]].__send__(cond.method_name, v.to_s)
            end
          end
        end
      }
      if value
        @table.new(@table.keys.zip(value).to_h)
      else
        nil
      end
    end

    def last
      to_a.last
    end

    def each
      return to_enum(:each) unless block_given?

      keys = @table.keys
      key_to_value_index = keys.each_with_index.map { |k, index| [k, index] }.to_h
      values = @table.open { |csv|
        csv.gets
        csv.select do |value|
          @conditions.all? do |cond|
            cond.values.all? do |k, v|
              value[key_to_value_index[k]].__send__(cond.method_name, v.to_s)
            end
          end
        end
      }
      values.each do |value|
        yield @table.new(@table.keys.zip(value).to_h)
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10365659777424484, 'fraction_numerical': 0.0, 'mean_word_length': 1.757230499561788, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '328093', 'n_tokens_mistral': 1047, 'n_tokens_neox': 1020, 'n_words': 239}