<commit_before>require 'delegate'

module CSVH
  class Reader
    extend Forwardable

    DEFAULT_CSV_OPTS = {
      headers: :first_row,
      return_headers: true
    }.freeze

    class << self
      def from_file(file_path, **opts)
        opts = DEFAULT_CSV_OPTS.merge(opts)
        io = File.open(file_path, 'r')
        csv = CSV.new(io, **opts)
        new(csv)
      end

      def from_string_or_io(data, **opts)
        opts = DEFAULT_CSV_OPTS.merge(opts)
        csv = CSV.new(data, **opts)
        new(csv)
      end

      alias foreach from_file
      alias parse from_string_or_io
    end

    def initialize(csv)
      unless csv.return_headers?
        raise \
          InappropreateCsvInstanceError,
           "%{self.class} requires a CSV instance that returns headers." \
          " It needs to have been initialized with non-false/nil values" \
          " for :headers and :return_headers options."
      end
      @csv = csv
    end

    def to_csvh
      self
    end

    def headers
      @headers ||= begin
        row = @csv.readline
        unless row.header_row?
          raise \
            CsvPrematurelyShiftedError,
            "the header row was prematurely read from the underlying CSV object."
        end
        row.headers
      end
    end

    def_delegators \
      :@csv,
      :close

    def each
      headers
      if block_given?
        @csv.each { |row| yield row }
      else
        @csv.each
      end
    end
  end
end
<commit_msg>whitespace<commit_after>require 'delegate'

module CSVH

  class Reader
    extend Forwardable

    DEFAULT_CSV_OPTS = {
      headers: :first_row,
      return_headers: true
    }.freeze

    class << self
      def from_file(file_path, **opts)
        opts = DEFAULT_CSV_OPTS.merge(opts)
        io = File.open(file_path, 'r')
        csv = CSV.new(io, **opts)
        new(csv)
      end

      def from_string_or_io(data, **opts)
        opts = DEFAULT_CSV_OPTS.merge(opts)
        csv = CSV.new(data, **opts)
        new(csv)
      end

      alias foreach from_file
      alias parse from_string_or_io
    end

    def initialize(csv)
      unless csv.return_headers?
        raise \
          InappropreateCsvInstanceError,
           "%{self.class} requires a CSV instance that returns headers." \
          " It needs to have been initialized with non-false/nil values" \
          " for :headers and :return_headers options."
      end
      @csv = csv
    end

    def to_csvh
      self
    end

    def headers
      @headers ||= begin
        row = @csv.readline
        unless row.header_row?
          raise \
            CsvPrematurelyShiftedError,
            "the header row was prematurely read from the underlying CSV object."
        end
        row.headers
      end
    end

    def_delegators \
      :@csv,
      :close

    def each
      headers
      if block_given?
        @csv.each { |row| yield row }
      else
        @csv.each
      end
    end

  end

end

{'fraction_non_alphanumeric': 0.07573726541554959, 'fraction_numerical': 0.0, 'mean_word_length': 1.7285191956124315, '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': '3697095', 'n_tokens_mistral': 950, 'n_tokens_neox': 903, 'n_words': 250}