lib/mail/configuration.rb in mail-1.6.0 vs lib/mail/configuration.rb in mail-2.0.3

- old
+ new

@@ -5,136 +5,60 @@ require 'singleton' module Mail # The Configuration class is a Singleton used to hold the default - # configuration for all class wide configurations of Mail. + # configuration for all Mail objects. # - # This includes things like running in Test mode, the POP3, IMAP, - # SMTP, Sendmail and File delivery method information et al. - # - # See Mail.defaults for more information. + # Each new mail object gets a copy of these values at initialization + # which can be overwritten on a per mail object basis. class Configuration include Singleton - # Creates a new Mail::Configuration object through .defaults - def defaults(&block) - if block_given? - instance_eval(&block) - end - self + @delivery_method = nil + @retriever_method = nil + + def delivery_method(method = nil, settings = {}) + return @delivery_method if @delivery_method && method.nil? + @delivery_method = lookup_delivery_method(method).new(settings) end - - # Allows you to specify the SMTP Server by passing the hostname - # or IP Address as a String with an optional port number as a - # string or integer. - # - # Defaults to 127.0.0.1 and port 25. - # - # Example: - # - # Mail.defaults.do - # smtp '127.0.0.1' - # end - # - # Mail.defaults do - # smtp '127.0.0.1', 25 - # end - def smtp(*args, &block) - if args.size > 0 - host_array = [args[0], (args[1].to_i > 0 ? args[1] : 25)] + + def lookup_delivery_method(method) + case method + when nil + Mail::SMTP + when :smtp + Mail::SMTP + when :sendmail + Mail::Sendmail + when :file + Mail::FileDelivery + when :test + Mail::TestMailer + else + method end - set_settings(Mail::SMTP, host_array, &block) end - # Allows you to define the delivery method for mail, defaults to SMTP - # - # This can either be a symbol (:smtp, :sendmail, :file, :test) or you can pass - # in your own delivery class, in which case this will be set. - def delivery_method(value = nil) - value ? @delivery_method = lookup_delivery_method(value) : @delivery_method ||= Mail::SMTP + def retriever_method(method = nil, settings = {}) + return @retriever_method if @retriever_method && method.nil? + @retriever_method = lookup_retriever_method(method).new(settings) end - # Allows you to specify the POP3 Server by passing the hostname - # or IP Address as a String with an optional port number as a - # string or integer. - # - # Defaults to 127.0.0.1 and port 110. - # - # Example: - # - # Mail.defaults.do - # pop3 '127.0.0.1' - # end - # - # Mail.defaults do - # pop3 '127.0.0.1', 995 do - # enable_tls - # end - # end - def pop3(*args, &block) - if args.size > 0 - host_array = [args[0], (args[1].to_i > 0 ? args[1] : 110)] + def lookup_retriever_method(method) + case method + when nil + Mail::POP3 + when :pop3 + Mail::POP3 + else + method end - set_settings(Mail::POP3, host_array, &block) end - def sendmail(sendmail_path = nil) - delivery_method :sendmail - set_settings(Mail::Sendmail) do - path sendmail_path - end - end - - # Allows you to define the retriever method for mail, defaults to POP3 - # - # This can either be a symbol (:pop3, :imap) or you can pass - # in your own retriever class, in which case this will be set. - def retriever_method(value = nil) - value ? @retriever_method = lookup_retriever_method(value) : @retriever_method ||= Mail::POP3 - end - def param_encode_language(value = nil) value ? @encode_language = value : @encode_language ||= 'en' end - - private - - def set_settings(klass, host_array = nil, &block) - if host_array - klass.instance.settings do - host host_array[0] - port host_array[1] - end - end - klass.instance.settings(&block) - end - def lookup_delivery_method(method) - case method - when :smtp || nil - Mail::SMTP - when :sendmail - Mail::Sendmail - when :file - Mail::FileDelivery - when :test - Mail::TestMailer - else - method - end - end - - def lookup_retriever_method(method) - case method - when :pop3 || nil - Mail::POP3 - # when :imap - # Mail::IMAP - else - method - end - end - end end \ No newline at end of file