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


30
31
32
# File 'lib/mail/fields/common_date_field.rb', line 30

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

Class Method Details

.normalize_datetime(string) ⇒ Object


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

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


7
8
9
# File 'lib/mail/fields/common_date_field.rb', line 7

def self.singular?
  true
end

Instance Method Details

#date_timeObject

Returns a date time object of the parsed date


35
36
37
38
39
# File 'lib/mail/fields/common_date_field.rb', line 35

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

#defaultObject


41
42
43
# File 'lib/mail/fields/common_date_field.rb', line 41

def default
  date_time
end

#elementObject


45
46
47
# File 'lib/mail/fields/common_date_field.rb', line 45

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