Sha256: c87f174e80a0a09a856fafd9d836dad74fbab0ea38412a7b36369e9a8ad4226b

Contents?: true

Size: 1.26 KB

Versions: 16

Compression:

Stored size: 1.26 KB

Contents

require 'mail/fields/named_structured_field'
require 'mail/elements/date_time_element'
require 'mail/utilities'

module Mail
  class CommonDateField < NamedStructuredField #:nodoc:
    def self.singular?
      true
    end

    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

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

    # Returns a date time object of the parsed date
    def date_time
      ::DateTime.parse("#{element.date_string} #{element.time_string}")
    rescue ArgumentError => e
      raise e unless e.message == 'invalid date'
    end

    def default
      date_time
    end

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

    private
      def do_encode
        "#{name}: #{value}\r\n"
      end

      def do_decode
        value.to_s
      end
  end
end

Version data entries

16 entries across 15 versions & 6 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.1.0/gems/mail-2.8.1/lib/mail/fields/common_date_field.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/mail-2.8.1/lib/mail/fields/common_date_field.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/mail-2.8.1/lib/mail/fields/common_date_field.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/mail-2.8.1/lib/mail/fields/common_date_field.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/mail-2.8.1/lib/mail/fields/common_date_field.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/mail-2.8.1/lib/mail/fields/common_date_field.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/mail-2.8.1/lib/mail/fields/common_date_field.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/mail-2.8.1/lib/mail/fields/common_date_field.rb
mail-2.8.1 lib/mail/fields/common_date_field.rb
mail-2.8.1.rc2 lib/mail/fields/common_date_field.rb
mail-2.8.0.1 lib/mail/fields/common_date_field.rb
mail-2.8.0.1.rc1 lib/mail/fields/common_date_field.rb
mail-2.8.0 lib/mail/fields/common_date_field.rb
mail-2.8.0.rc3 lib/mail/fields/common_date_field.rb
mail-2.8.0.rc2 lib/mail/fields/common_date_field.rb
mail-2.8.0.rc1 lib/mail/fields/common_date_field.rb