<commit_before>module Norm
  class Record
    class Collection
      attr_reader :records

      def initialize(records = nil)
        @records = Array(records).dup.freeze
      end

      def record_class
        Record
      end

      def insert_attributes(attributes)
        attributes = Array(attributes)
        if attributes.size != records.size
          raise ArgumentError,
            "#{attributes.size} attribute sets, but #{records.size} records"
        end
        records.zip(attributes).each do |record, attrs|
          record.set_attributes(attrs)
        end
        self
      end

      def set_attributes(attributes)
        return self unless records.any?
        matching = Hash[attributes.map { |attrs|
          record = record_class.new(attrs)
          [record, record]
        }]
        records.each do |record|
          if match = matching[record]
            record.set_attributes(match.initialized_attributes)
          end
        end
        self
      end

      def set(attributes)
        return self unless records.any?
        matching = Hash[attributes.map { |attrs|
          record = record_class.new(attrs)
          [record, record]
        }]
        records.each do |record|
          if match = matching[record]
            record.set(match.initialized_attributes)
          end
        end
        self
      end

      def stored!
        records.each(&:stored!)
        self
      end

      def inserted!
        records.each(&:inserted!)
        self
      end

      def updated!
        records.each(&:updated!)
        self
      end

      def deleted!
        records.each(&:deleted!)
        self
      end

      def valid?
        records.all?(&:valid?)
      end

      def constraint_rule_for(error)
        nil
      end

      def constraint_error(error)
        nil
      end

      def to_a
        records.dup
      end

      def each(&block)
        records.each(&block)
      end

      def empty?
        records.empty?
      end

      def size
        records.size
      end

      def first
        records.first
      end

      def last
        records.last
      end

      def [](index)
        records[index]
      end

      def ==(other)
        !!(self.class <=> other.class) &&
          self.records == other.records
      end

      def eql?(other)
        self.class.eql?(other.class) &&
          self.records.eql?(other.records)
      end

      def hash
        self.class.hash ^ self.records.hash
      end

      private

    end

    def self.Collection(records)
      if Collection === records
        records
      else
        records = Array(records)<commit_msg>Extract method for duplicate code in Collection<commit_after>module Norm
  class Record
    class Collection
      attr_reader :records

      def initialize(records = nil)
        @records = Array(records).dup.freeze
      end

      def record_class
        Record
      end

      def insert_attributes(attributes)
        attributes = Array(attributes)
        if attributes.size != records.size
          raise ArgumentError,
            "#{attributes.size} attribute sets, but #{records.size} records"
        end
        records.zip(attributes).each do |record, attrs|
          record.set_attributes(attrs)
        end
        self
      end

      def set_attributes(attributes)
        return self unless records.any?
        matching = record_map(attributes)
        records.each do |record|
          if match = matching[record]
            record.set_attributes(match.initialized_attributes)
          end
        end
        self
      end

      def set(attributes)
        return self unless records.any?
        matching = record_map(attributes)
        records.each do |record|
          if match = matching[record]
            record.set(match.initialized_attributes)
          end
        end
        self
      end

      def stored!
        records.each(&:stored!)
        self
      end

      def inserted!
        records.each(&:inserted!)
        self
      end

      def updated!
        records.each(&:updated!)
        self
      end

      def deleted!
        records.each(&:deleted!)
        self
      end

      def valid?
        records.all?(&:valid?)
      end

      def constraint_rule_for(error)
        nil
      end

      def constraint_error(error)
        nil
      end

      def to_a
        records.dup
      end

      def each(&block)
        records.each(&block)
      end

      def empty?
        records.empty?
      end

      def size
        records.size
      end

      def first
        records.first
      end

      def last
        records.last
      end

      def [](index)
        records[index]
      end

      def ==(other)
        !!(self.class <=> other.class) &&
          self.records == other.records
      end

      def eql?(other)
        self.class.eql?(other.class) &&
          self.records.eql?(other.records)
      end

      def hash
        self.class.hash ^ self.records.hash
      end

      private

      def record_map(attributes)
        Hash[
          attributes.map { |attrs|
            record = record_class.new(attrs)
            [record, record]
          }
        ]
      end

    end

    def self.Collection(records)
      if Collection === records
        records
      else
        records = Array(records)
{'fraction_non_alphanumeric': 0.07742415782616788, 'fraction_numerical': 0.0, 'mean_word_length': 1.4833641404805915, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1980289', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1416, 'n_words': 361}