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: read_preference_supported.rb
    path of file: ./repos/mongo-ruby-driver/lib/mongo/operation/shared
    
    the code of the file until where you have to start completion: # frozen_string_literal: true
# rubocop:todo all

# Copyright (C) 2015-2020 MongoDB Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");

    
      def add_read_preference_legacy(sel, connection)
        if read && (
          connection.description.mongos? || connection.description.load_balancer?
        ) && read_pref = read.to_mongos
          # If the read preference contains only mode and mode is secondary
          # preferred and we are sending to a pre-OP_MSG server, this read
          # preference is indicated by the :secondary_ok wire protocol flag
          # and $readPreference command parameter isn't sent.
          if read_pref != {mode: 'secondaryPreferred'}
            Mongo::Lint.validate_camel_case_read_preference(read_pref)
            sel = sel[:$query] ? sel : {:$query => sel}
            sel = sel.merge(:$readPreference => read_pref)
          end