Class: Mail::CommonDateField
- Inherits:
-
NamedStructuredField
- Object
- CommonField
- StructuredField
- NamedStructuredField
- Mail::CommonDateField
- Defined in:
- lib/mail/fields/common_date_field.rb
Overview
:nodoc:
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from CommonField
#charset, #errors, #name, #value
Class Method Summary collapse
Instance Method Summary collapse
-
#date_time ⇒ Object
Returns a date time object of the parsed date.
- #default ⇒ Object
- #element ⇒ Object
-
#initialize(value = nil, charset = nil) ⇒ CommonDateField
constructor
A new instance of CommonDateField.
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. end end if datetime datetime.strftime('%a, %d %b %Y %H:%M:%S %z') else string end end |
.singular? ⇒ Boolean
5 6 7 |
# File 'lib/mail/fields/common_date_field.rb', line 5 def self.singular? true end |
Instance Method Details
#date_time ⇒ Object
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. == 'invalid date' end |
#default ⇒ Object
39 40 41 |
# File 'lib/mail/fields/common_date_field.rb', line 39 def default date_time end |
#element ⇒ Object
43 44 45 |
# File 'lib/mail/fields/common_date_field.rb', line 43 def element @element ||= Mail::DateTimeElement.new(value) end |