Sha256: bfa3c13d2dd872a4f04ba8f86c33c7e757aa2947e4a155aa7c9c9a76db7097ca

Contents?: true

Size: 1.15 KB

Versions: 9

Compression:

Stored size: 1.15 KB

Contents

module Scrivito

  # Adds support for string columns which contain ISO dates
  module DateAttribute
    def self.deserialize_from_backend(iso_date_time)
      return nil unless iso_date_time

      if iso_date_time.to_s =~ /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/
        Time.utc($1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i).in_time_zone
      else
        raise "The value is not a valid ISO date time: #{iso_date_time.inspect}"
      end
    end

    def self.deserialize_from_client(iso8601_date_time)
      return unless iso8601_date_time

      DateTime.iso8601(iso8601_date_time).in_time_zone
    rescue ArgumentError
      raise Scrivito::ClientError.new(
        "The value is not a valid ISO 8601 date time: #{iso8601_date_time.inspect}",
        400
      )
    end

    def self.serialize_for_backend(attribute_value)
      attribute_value = case attribute_value
        when Date then attribute_value.to_time
        when Time then attribute_value.to_time.utc
        end
      attribute_value.strftime('%Y%m%d%H%M%S') if attribute_value
    end

    def self.serialize_for_client(attribute_value)
      attribute_value.utc.iso8601
    end
  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
scrivito_sdk-1.2.0 lib/scrivito/date_attribute.rb
scrivito_sdk-1.2.0.rc3 lib/scrivito/date_attribute.rb
scrivito_sdk-1.2.0.rc2 lib/scrivito/date_attribute.rb
scrivito_sdk-1.2.0.rc1 lib/scrivito/date_attribute.rb
scrivito_sdk-1.1.1 lib/scrivito/date_attribute.rb
scrivito_sdk-1.1.0 lib/scrivito/date_attribute.rb
scrivito_sdk-1.1.0.rc3 lib/scrivito/date_attribute.rb
scrivito_sdk-1.1.0.rc2 lib/scrivito/date_attribute.rb
scrivito_sdk-1.1.0.rc1 lib/scrivito/date_attribute.rb