Sha256: 475b4d8d170ea11ede21a8afaf579129fc6a29f447853aeb21c34f1a614412d2

Contents?: true

Size: 696 Bytes

Versions: 3

Compression:

Stored size: 696 Bytes

Contents

module Reactor
  module Attributes
    class DateSerializer
      def initialize(attr, value)
        @attr, @value = attr, value
      end

      def serialize
        @serialized ||= serialize_date
      end

      private
      def serialize_date
        if @value.is_a?(Time)
          @value.utc.to_iso
        elsif @value.is_a?(String)
          if iso_format?(@value)
            @value
          elsif !@value.empty?
            Time.zone.parse(@value).utc.to_iso
          else
            # empty string <=> clear date
            nil
          end
        else
          @value
        end
      end

      def iso_format?(val)
        val =~ /^[0-9]{14}$/
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
infopark_reactor-1.6.1 lib/reactor/attributes/date_serializer.rb
infopark_reactor-1.5.2 lib/reactor/attributes/date_serializer.rb
infopark_reactor-1.5.1 lib/reactor/attributes/date_serializer.rb