Class: Mail::CommonDateField

Inherits:
NamedStructuredField show all
Defined in:
lib/mail/fields/common_date_field.rb

Overview

:nodoc:

Direct Known Subclasses

DateField, ResentDateField

Instance Attribute Summary

Attributes inherited from CommonField

#charset, #errors, #name, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CommonField

#decoded, #encoded, #parse, parse, #responsible_for?, #singular?, #to_s

Constructor Details

#initialize(value = nil, charset = nil) ⇒ CommonDateField

Returns a new instance of CommonDateField.



28
29
30
# File 'lib/mail/fields/common_date_field.rb', line 28

def initialize(value = nil, charset = nil)
  super self.class.normalize_datetime(value), charset
end

Class Method Details

.normalize_datetime(string) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mail/fields/common_date_field.rb', line 9

def self.normalize_datetime(string)
  if Utilities.blank?(string)
    datetime = ::DateTime.now
  else
    stripped = string.to_s.gsub(/\(.*?\)/, '').squeeze(' ')
    begin
      datetime = ::DateTime.parse(stripped)
    rescue ArgumentError => e
      raise unless 'invalid date' == e.message
    end
  end

  if datetime
    datetime.strftime('%a, %d %b %Y %H:%M:%S %z')
  else
    string
  end
end

.singular?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/mail/fields/common_date_field.rb', line 5

def self.singular?
  true
end

Instance Method Details

#date_timeObject

Returns a date time object of the parsed date



33
34
35
36
37
# File 'lib/mail/fields/common_date_field.rb', line 33

def date_time
  ::DateTime.parse("#{element.date_string} #{element.time_string}")
rescue ArgumentError => e
  raise e unless e.message == 'invalid date'
end

#defaultObject



39
40
41
# File 'lib/mail/fields/common_date_field.rb', line 39

def default
  date_time
end

#elementObject



43
44
45
# File 'lib/mail/fields/common_date_field.rb', line 43

def element
  @element ||= Mail::DateTimeElement.new(value)
end