Sha256: 482e7c98f94f0c77b8fa0b65adefdff848c55c390100f13984cf325366035a1b

Contents?: true

Size: 1.55 KB

Versions: 23

Compression:

Stored size: 1.55 KB

Contents

# encoding: utf-8
# frozen_string_literal: true
module Mail
  module Encodings
    class TransferEncoding
      NAME = ''

      PRIORITY = -1

      def self.can_transport?(enc)
        enc = Encodings.get_name(enc)
        if Encodings.defined? enc
          Encodings.get_encoding(enc).new.is_a? self
        else
          false
        end
      end

      def self.can_encode?(enc)
        can_transport? enc 
      end

      def self.cost(str)
        raise "Unimplemented"
      end

      def self.compatible_input?(str)
        true
      end

      def self.to_s
        self::NAME
      end

      def self.get_best_compatible(source_encoding, str)
        if self.can_transport?(source_encoding) && self.compatible_input?(str)
          source_encoding
        else
          choices = Encodings.get_all.select do |enc|
            self.can_transport?(enc) && enc.can_encode?(source_encoding)
          end

          best = nil
          best_cost = nil

          choices.each do |enc|
            # If the current choice cannot be transported safely,
            # give priority to other choices but allow it to be used as a fallback.
            this_cost = enc.cost(str) if enc.compatible_input?(str)

            if !best_cost || (this_cost && this_cost < best_cost)
              best_cost = this_cost
              best = enc
            elsif this_cost == best_cost
              best = enc if enc::PRIORITY < best::PRIORITY
            end
          end

          best
        end
      end

      def to_s
        self.class.to_s
      end
    end
  end
end

Version data entries

23 entries across 21 versions & 6 rubygems

Version Path
tdiary-5.0.6 vendor/bundle/gems/mail-2.6.6/lib/mail/encodings/transfer_encoding.rb
tdiary-5.0.5 vendor/bundle/gems/mail-2.6.4/lib/mail/encodings/transfer_encoding.rb
tdiary-5.0.5 vendor/bundle/gems/mail-2.6.6/lib/mail/encodings/transfer_encoding.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/mail-2.6.4/lib/mail/encodings/transfer_encoding.rb
mail-2.6.6 lib/mail/encodings/transfer_encoding.rb
mail-2.7.0.rc1 lib/mail/encodings/transfer_encoding.rb
mail-2.6.6.rc1 lib/mail/encodings/transfer_encoding.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/mail-2.6.5/lib/mail/encodings/transfer_encoding.rb
mail-2.6.5 lib/mail/encodings/transfer_encoding.rb
mail-2.6.5.rc1 lib/mail/encodings/transfer_encoding.rb
tdiary-5.0.4 vendor/bundle/gems/mail-2.6.4/lib/mail/encodings/transfer_encoding.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/mail-2.6.4/lib/mail/encodings/transfer_encoding.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/mail-2.6.4/lib/mail/encodings/transfer_encoding.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/mail-2.6.4/lib/mail/encodings/transfer_encoding.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/mail-2.6.4/lib/mail/encodings/transfer_encoding.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/mail-2.6.4/lib/mail/encodings/transfer_encoding.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/mail-2.6.4/lib/mail/encodings/transfer_encoding.rb
autocompl-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/mail-2.6.4/lib/mail/encodings/transfer_encoding.rb
abaci-0.3.0 vendor/bundle/gems/mail-2.6.4/lib/mail/encodings/transfer_encoding.rb
tdiary-5.0.2 vendor/bundle/gems/mail-2.6.4/lib/mail/encodings/transfer_encoding.rb