Skip to content

Instantly share code, notes, and snippets.

@iGEL
Created March 3, 2017 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iGEL/2faf44ba91542848bb411a23e5df66f5 to your computer and use it in GitHub Desktop.
Save iGEL/2faf44ba91542848bb411a23e5df66f5 to your computer and use it in GitHub Desktop.
Safe monkey patched mail 2.6.4 deprecation warnings for Ruby 2.4
# frozen_string_literal: true
unless Mail::VERSION.version == "2.6.4"
raise "Please delete #{__FILE__}, it's not required anymore"
end
# This file monkey patches away the new deprecation warnings from Ruby 2.4.
# Lifted here:
# * https://github.com/mikel/mail/pull/1053
# * https://github.com/mikel/mail/pull/1058
#
# The methods have been pulled from the 2.6.4 tag and then manually patched
# rubocop:disable all
module Mail
class AttachmentsList
def [](index_value)
if index_value.is_a?(Integer)
self.fetch(index_value)
else
self.select { |a| a.filename == index_value }.first
end
end
end
module Multibyte #:nodoc:
def []=(*args)
replace_by = args.pop
# Indexed replace with regular expressions already works
if args.first.is_a?(Regexp)
@wrapped_string[*args] = replace_by
else
result = Unicode.u_unpack(@wrapped_string)
if args[0].is_a?(Integer)
raise IndexError, "index #{args[0]} out of string" if args[0] >= result.length
min = args[0]
max = args[1].nil? ? min : (min + args[1] - 1)
range = Range.new(min, max)
replace_by = [replace_by].pack('U') if replace_by.is_a?(Integer)
elsif args.first.is_a?(Range)
raise RangeError, "#{args[0]} out of range" if args[0].min >= result.length
range = args[0]
else
needle = args[0].to_s
min = index(needle)
max = min + Unicode.u_unpack(needle).length - 1
range = Range.new(min, max)
end
result[range] = Unicode.u_unpack(replace_by)
@wrapped_string.replace(result.pack('U*'))
end
end
end
class TestRetriever
def find(options = {}, &block)
options[:count] ||= :all
options[:order] ||= :asc
options[:what] ||= :first
emails_index = (0...@@emails.size).to_a
emails_index.reverse! if options[:what] == :last
emails_index = case count = options[:count]
when :all then emails_index
when Integer then emails_index[0, count]
else
raise 'Invalid count option value: ' + count.inspect
end
if options[:what] == :last && options[:order] == :asc || options[:what] == :first && options[:order] == :desc
emails_index.reverse!
end
emails_index.each { |idx| @@emails[idx].mark_for_delete = true } if options[:delete_after_find]
emails = emails_index.map { |idx| @@emails[idx] }
emails.each { |email| yield email } if block_given?
@@emails.reject!(&:is_marked_for_delete?) if options[:delete_after_find]
emails.size == 1 && options[:count] == 1 ? emails.first : emails
end
end
class AddressContainer
def <<(address)
@field << address
end
end
end
# rubocop:enable all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment