mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-24 12:06:22 +00:00
56 lines
1 KiB
Ruby
56 lines
1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# Atributo de fechas
|
|
class MetadataDate < MetadataTemplate
|
|
include Metadata::NonIndexableConcern
|
|
|
|
# La fecha de hoy
|
|
#
|
|
# @return [Date]
|
|
def default_value
|
|
super || Date.today
|
|
end
|
|
|
|
def validate
|
|
super
|
|
|
|
begin
|
|
sanitize value
|
|
rescue Date::Error
|
|
errors << I18n.t('metadata.date.invalid_format')
|
|
end
|
|
|
|
errors.empty?
|
|
end
|
|
|
|
# Devuelve una fecha, si no hay ninguna es la fecha de hoy.
|
|
#
|
|
# @return [Date]
|
|
def save
|
|
return true unless changed?
|
|
|
|
self[:value] = sanitize(self[:value]) || document_value || default_value
|
|
|
|
true
|
|
end
|
|
|
|
private
|
|
|
|
# Devuelve una fecha o nada. Sigue el formato de fechas de Jekyll,
|
|
# con lo que no acepta fechas en el futuro lejano.
|
|
#
|
|
# @param :value [Any]
|
|
# @return [Date,nil]
|
|
def sanitize(value)
|
|
case value
|
|
when Date then value
|
|
when Time then value.to_date
|
|
when String
|
|
value = super(value)
|
|
|
|
raise Date::Error unless /\A\d{2,4}-\d{1,2}-\d{1,2}\z/ =~ value
|
|
|
|
Date.iso8601(value)
|
|
end
|
|
end
|
|
end
|