Skip to content

Commit bf02b67

Browse files
authored
Merge pull request #1491 from c960657/autoload-fields
Lazy-load fields and elements
2 parents f95e32a + dd57731 commit bf02b67

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+106
-79
lines changed

CHANGELOG.rdoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
== Version 2.9.0 (unreleased)
22

33
Breaking changes:
4+
* Mail::Field::FIELDS_MAP now contains class names, not Class instances (c960657)
45

56
Compatibility:
67

lib/mail.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def self.eager_autoload!
4848
require 'mail/field'
4949
require 'mail/field_list'
5050

51-
require 'mail/envelope'
51+
register_autoload :Envelope, 'mail/envelope'
5252

5353
# Autoload header field elements and transfer encodings.
5454
require 'mail/elements'
@@ -58,7 +58,7 @@ def self.eager_autoload!
5858
require 'mail/encodings/unix_to_unix'
5959

6060
require 'mail/matchers/has_sent_mail'
61-
require 'mail/matchers/attachment_matchers.rb'
61+
require 'mail/matchers/attachment_matchers'
6262

6363
# Finally... require all the Mail.methods
6464
require 'mail/mail'

lib/mail/field.rb

Lines changed: 64 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,40 +36,68 @@ class Field
3636
KNOWN_FIELDS = STRUCTURED_FIELDS + ['comments', 'subject']
3737

3838
FIELDS_MAP = {
39-
"to" => ToField,
40-
"cc" => CcField,
41-
"bcc" => BccField,
42-
"message-id" => MessageIdField,
43-
"in-reply-to" => InReplyToField,
44-
"references" => ReferencesField,
45-
"subject" => SubjectField,
46-
"comments" => CommentsField,
47-
"keywords" => KeywordsField,
48-
"date" => DateField,
49-
"from" => FromField,
50-
"sender" => SenderField,
51-
"reply-to" => ReplyToField,
52-
"resent-date" => ResentDateField,
53-
"resent-from" => ResentFromField,
54-
"resent-sender" => ResentSenderField,
55-
"resent-to" => ResentToField,
56-
"resent-cc" => ResentCcField,
57-
"resent-bcc" => ResentBccField,
58-
"resent-message-id" => ResentMessageIdField,
59-
"return-path" => ReturnPathField,
60-
"received" => ReceivedField,
61-
"mime-version" => MimeVersionField,
62-
"content-transfer-encoding" => ContentTransferEncodingField,
63-
"content-description" => ContentDescriptionField,
64-
"content-disposition" => ContentDispositionField,
65-
"content-type" => ContentTypeField,
66-
"content-id" => ContentIdField,
67-
"content-location" => ContentLocationField,
39+
"to" => "ToField",
40+
"cc" => "CcField",
41+
"bcc" => "BccField",
42+
"message-id" => "MessageIdField",
43+
"in-reply-to" => "InReplyToField",
44+
"references" => "ReferencesField",
45+
"subject" => "SubjectField",
46+
"comments" => "CommentsField",
47+
"keywords" => "KeywordsField",
48+
"date" => "DateField",
49+
"from" => "FromField",
50+
"sender" => "SenderField",
51+
"reply-to" => "ReplyToField",
52+
"resent-date" => "ResentDateField",
53+
"resent-from" => "ResentFromField",
54+
"resent-sender" => "ResentSenderField",
55+
"resent-to" => "ResentToField",
56+
"resent-cc" => "ResentCcField",
57+
"resent-bcc" => "ResentBccField",
58+
"resent-message-id" => "ResentMessageIdField",
59+
"return-path" => "ReturnPathField",
60+
"received" => "ReceivedField",
61+
"mime-version" => "MimeVersionField",
62+
"content-transfer-encoding" => "ContentTransferEncodingField",
63+
"content-description" => "ContentDescriptionField",
64+
"content-disposition" => "ContentDispositionField",
65+
"content-type" => "ContentTypeField",
66+
"content-id" => "ContentIdField",
67+
"content-location" => "ContentLocationField",
6868
}
6969

70-
FIELD_NAME_MAP = FIELDS_MAP.inject({}) do |map, (field, field_klass)|
71-
map.update(field => field_klass::NAME)
72-
end
70+
FIELD_NAME_MAP = {
71+
"to" => "To",
72+
"cc" => "Cc",
73+
"bcc" => "Bcc",
74+
"message-id" => "Message-ID",
75+
"in-reply-to" => "In-Reply-To",
76+
"references" => "References",
77+
"subject" => "Subject",
78+
"comments" => "Comments",
79+
"keywords" => "Keywords",
80+
"date" => "Date",
81+
"from" => "From",
82+
"sender" => "Sender",
83+
"reply-to" => "Reply-To",
84+
"resent-date" => "Resent-Date",
85+
"resent-from" => "Resent-From",
86+
"resent-sender" => "Resent-Sender",
87+
"resent-to" => "Resent-To",
88+
"resent-cc" => "Resent-Cc",
89+
"resent-bcc" => "Resent-Bcc",
90+
"resent-message-id" => "Resent-Message-ID",
91+
"return-path" => "Return-Path",
92+
"received" => "Received",
93+
"mime-version" => "MIME-Version",
94+
"content-transfer-encoding" => "Content-Transfer-Encoding",
95+
"content-description" => "Content-Description",
96+
"content-disposition" => "Content-Disposition",
97+
"content-type" => "Content-Type",
98+
"content-id" => "Content-ID",
99+
"content-location" => "Content-Location",
100+
}
73101

74102
# Generic Field Exception
75103
class FieldError < StandardError
@@ -145,7 +173,8 @@ def split(raw_field) #:nodoc:
145173
end
146174

147175
def field_class_for(name) #:nodoc:
148-
FIELDS_MAP[name.to_s.downcase]
176+
class_name = FIELDS_MAP[name.to_s.downcase]
177+
Mail.const_get(class_name) if class_name
149178
end
150179
end
151180

@@ -176,7 +205,8 @@ def initialize(name, value = nil, charset = 'utf-8')
176205
@unparsed_value = value
177206
@charset = charset
178207
end
179-
@name = FIELD_NAME_MAP[@name.to_s.downcase] || @name
208+
klass = self.class.field_class_for(@name)
209+
@name = klass ? klass::NAME : @name
180210
end
181211

182212
def field=(field)

lib/mail/fields.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# frozen_string_literal: true
22
module Mail
3+
register_autoload :CommonField, 'mail/fields/common_field'
4+
register_autoload :CommonAddressField, 'mail/fields/common_address_field'
5+
register_autoload :CommonMessageIdField, 'mail/fields/common_message_id_field'
6+
register_autoload :CommonDateField, 'mail/fields/common_date_field'
7+
38
register_autoload :UnstructuredField, 'mail/fields/unstructured_field'
9+
register_autoload :NamedUnstructuredField, 'mail/fields/named_unstructured_field'
410
register_autoload :StructuredField, 'mail/fields/structured_field'
511
register_autoload :OptionalField, 'mail/fields/optional_field'
612

@@ -19,6 +25,7 @@ module Mail
1925
register_autoload :KeywordsField, 'mail/fields/keywords_field'
2026
register_autoload :MessageIdField, 'mail/fields/message_id_field'
2127
register_autoload :MimeVersionField, 'mail/fields/mime_version_field'
28+
register_autoload :NamedStructuredField, 'mail/fields/named_structured_field'
2229
register_autoload :ReceivedField, 'mail/fields/received_field'
2330
register_autoload :ReferencesField, 'mail/fields/references_field'
2431
register_autoload :ReplyToField, 'mail/fields/reply_to_field'
@@ -33,4 +40,6 @@ module Mail
3340
register_autoload :SenderField, 'mail/fields/sender_field'
3441
register_autoload :SubjectField, 'mail/fields/subject_field'
3542
register_autoload :ToField, 'mail/fields/to_field'
43+
44+
register_autoload :ParameterHash, 'mail/fields/parameter_hash'
3645
end

lib/mail/fields/bcc_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/common_address_field'
43

54
module Mail
65
# = Blind Carbon Copy Field

lib/mail/fields/cc_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/common_address_field'
43

54
module Mail
65
# = Carbon Copy Field

lib/mail/fields/comments_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/named_unstructured_field'
43

54
module Mail
65
# = Comments Field

lib/mail/fields/common_address_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/named_structured_field'
43

54
module Mail
65
class AddressContainer < Array #:nodoc:

lib/mail/fields/common_date_field.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
require 'mail/fields/named_structured_field'
2-
require 'mail/elements/date_time_element'
31
require 'mail/utilities'
42

53
module Mail

lib/mail/fields/common_message_id_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/named_structured_field'
43
require 'mail/utilities'
54

65
module Mail

lib/mail/fields/content_description_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/named_unstructured_field'
43

54
module Mail
65
class ContentDescriptionField < NamedUnstructuredField #:nodoc:

lib/mail/fields/content_disposition_field.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/named_structured_field'
4-
require 'mail/fields/parameter_hash'
53

64
module Mail
75
class ContentDispositionField < NamedStructuredField #:nodoc:

lib/mail/fields/content_id_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/named_structured_field'
43
require 'mail/utilities'
54

65
module Mail

lib/mail/fields/content_location_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/named_structured_field'
43

54
module Mail
65
class ContentLocationField < NamedStructuredField #:nodoc:

lib/mail/fields/content_transfer_encoding_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/named_structured_field'
43

54
module Mail
65
class ContentTransferEncodingField < NamedStructuredField #:nodoc:

lib/mail/fields/content_type_field.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/named_structured_field'
4-
require 'mail/fields/parameter_hash'
53

64
module Mail
75
class ContentTypeField < NamedStructuredField #:nodoc:

lib/mail/fields/date_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/common_date_field'
43

54
module Mail
65
# = Date Field

lib/mail/fields/from_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/common_address_field'
43

54
module Mail
65
# = From Field

lib/mail/fields/in_reply_to_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/common_message_id_field'
43

54
module Mail
65
# = In-Reply-To Field

lib/mail/fields/keywords_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/named_structured_field'
43

54
module Mail
65
# keywords = "Keywords:" phrase *("," phrase) CRLF

lib/mail/fields/message_id_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/common_message_id_field'
43
require 'mail/utilities'
54

65
module Mail

lib/mail/fields/mime_version_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/named_structured_field'
43
require 'mail/utilities'
54

65
module Mail

lib/mail/fields/named_structured_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/structured_field'
43

54
module Mail
65
class NamedStructuredField < StructuredField #:nodoc:

lib/mail/fields/named_unstructured_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/unstructured_field'
43

54
module Mail
65
class NamedUnstructuredField < UnstructuredField #:nodoc:

lib/mail/fields/optional_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/unstructured_field'
43

54
module Mail
65
# The field names of any optional-field MUST NOT be identical to any

lib/mail/fields/received_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/named_structured_field'
43

54
module Mail
65
# trace = [return]

lib/mail/fields/references_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/common_message_id_field'
43

54
module Mail
65
# = References Field

lib/mail/fields/reply_to_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/common_address_field'
43

54
module Mail
65
# = Reply-To Field

lib/mail/fields/resent_bcc_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/common_address_field'
43

54
module Mail
65
# = Resent-Bcc Field

lib/mail/fields/resent_cc_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/common_address_field'
43

54
module Mail
65
# = Resent-Cc Field

lib/mail/fields/resent_date_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/common_date_field'
43

54
module Mail
65
#

lib/mail/fields/resent_from_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/common_address_field'
43

54
module Mail
65
# = Resent-From Field

lib/mail/fields/resent_message_id_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/common_message_id_field'
43

54
module Mail
65
#

lib/mail/fields/resent_sender_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/common_address_field'
43

54
module Mail
65
# = Resent-Sender Field

lib/mail/fields/resent_to_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/common_address_field'
43

54
module Mail
65
# = Resent-To Field

lib/mail/fields/return_path_field.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
require 'mail/fields/common_address_field'
43

54
module Mail
65
# 4.4.3. REPLY-TO / RESENT-REPLY-TO

0 commit comments

Comments
 (0)