Sha256: b675f35fcf0f4b3e1ab6453c53170364cf40cd70f808429a06085ec47d06e8a7

Contents?: true

Size: 726 Bytes

Versions: 11

Compression:

Stored size: 726 Bytes

Contents

module Scrivito

  # Adds support for string columns which contain ISO dates
  module DateAttribute
    def self.parse(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.serialize(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
  end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
scrivito_sdk-0.66.0 lib/scrivito/date_attribute.rb
scrivito_sdk-0.66.0.rc2 lib/scrivito/date_attribute.rb
scrivito_sdk-0.65.2 lib/scrivito/date_attribute.rb
scrivito_sdk-0.66.0.rc1 lib/scrivito/date_attribute.rb
scrivito_sdk-0.65.1 lib/scrivito/date_attribute.rb
scrivito_sdk-0.65.0 lib/scrivito/date_attribute.rb
scrivito_sdk-0.65.0.rc2 lib/scrivito/date_attribute.rb
scrivito_sdk-0.65.0.rc1 lib/scrivito/date_attribute.rb
scrivito_sdk-0.60.0 lib/scrivito/date_attribute.rb
scrivito_sdk-0.60.0.rc2 lib/scrivito/date_attribute.rb
scrivito_sdk-0.60.0.rc1 lib/scrivito/date_attribute.rb