You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: eager.rb
    path of file: ./repos/mongoid/lib/mongoid/association/referenced/has_and_belongs_to_many
    
    the code of the file until where you have to start completion: # frozen_string_literal: true
# rubocop:todo all

module Mongoid
  module Association
    module Referenced
      class HasAndBelongsToMany

        # Eager class for has_and_belongs_to_many associations.
        class Eager < Association::Eager

          private

          def preload
            @docs.each do |d|
              set_relation(d, [])
            end

            entries = {}
            each_loaded_document do |doc|
              entries[doc.send(key)] = doc
            end

            @docs.each do |d|
              keys = d.send(group_by_key)
              docs = entries.values_at(*keys).compact
              set_relation(d, docs)
            end
          end

          def keys_from_docs
            keys = Set.new
            @docs.each do |d|
              keys += d.send(group_b
    
          def preload
            @docs.each do |d|
              set_relation(d, [])
            end

            entries = {}
            each_loaded_document do |doc|
              entries[doc.send(key)] = doc
            end

            @docs.each do |d|
              keys = d.send(group_by_key)
              docs = entries.values_at(*keys).compact
              set_relation(d, docs)
            end
          end

          def keys_from_docs
            keys = Set.new
            @docs.each do |d|
              keys += d.send(group_by_key)
            end
            keys.to_a
          end

          def set_relation(doc, element)
            doc.__build__(@association.name, element, @association) unless doc.blank?
          end

          def group_by_key
            @association.foreign_key
          end

          def key
            @association.primary_key
          end
        end
      end
    end