./lib/mail/field.rb in mail-1.0.0 vs ./lib/mail/field.rb in mail-1.1.0

- old
+ new

@@ -22,12 +22,12 @@ include Patterns include Comparable STRUCTURED_FIELDS = %w[ bcc cc content-description content-disposition - content-id content-transfer-encoding content-type - date from in-reply-to keywords message-id + content-id content-location content-transfer-encoding + content-type date from in-reply-to keywords message-id mime-version received references reply-to resent-bcc resent-cc resent-date resent-from resent-message-id resent-sender resent-to return-path sender to ] @@ -44,22 +44,37 @@ # Raised when attempting to set a structured field's contents to an invalid syntax class SyntaxError < FieldError #:nodoc: end - # Accepts a text string in the format of: + # Accepts a string: # - # "field-name: field data" + # Field.new("field-name: field data") # + # Or name, value pair: + # + # Field.new("field-name", "value") + # + # Or a name by itself: + # + # Field.new("field-name") + # # Note, does not want a terminating carriage return. Returns - # self appropriately parsed - def initialize(raw_field_text) - if raw_field_text !~ /:/ - name = raw_field_text + # self appropriately parsed. If value is not a string, then + # it will be passed through as is, for example, content-type + # field can accept an array with the type and a hash of + # parameters: + # + # Field.new('content-type', ['text', 'plain', {:charset => 'UTF-8'}]) + def initialize(name, value = nil) + case + when name =~ /:/ && value.blank? # Field.new("field-name: field data") + name, value = split(name) + create_field(name, value) + when name !~ /:/ && value.blank? # Field.new("field-name") create_field(name, nil) - else - name, value = split(raw_field_text) + else # Field.new("field-name", "value") create_field(name, value) end return self end @@ -110,11 +125,11 @@ resent-cc resent-bcc resent-message-id date from sender reply-to to cc bcc message-id in-reply-to references subject comments keywords mime-version content-type content-transfer-encoding - content-disposition content-description ] + content-location content-disposition content-description ] private def split(raw_field) match_data = raw_field.match(/^(#{FIELD_NAME})\s*:\s*(#{FIELD_BODY})$/) @@ -130,17 +145,17 @@ self.field = Mail::UnstructuredField.new(name, value) end end def new_field(name, value) - # Could do this with constantize and make it as DRY as, but a simple case is, - # well, simpler... + # Could do this with constantize and make it "as DRY as", but a simple case + # statement is, well, simpler... case name.downcase when /^to$/ - ToField.new(name,value) + ToField.new(name, value) when /^cc$/ - CcField.new(name,value) + CcField.new(name, value) when /^bcc$/ BccField.new(name, value) when /^message-id$/ MessageIdField.new(name, value) when /^in-reply-to$/ @@ -189,9 +204,11 @@ ContentDispositionField.new(name, value) when /^content-type$/ ContentTypeField.new(name, value) when /^content-id$/ ContentIdField.new(name, value) + when /^content-location$/ + ContentLocationField.new(name, value) else OptionalField.new(name, value) end end