5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-18 02:46:25 +00:00

Merge branch 'vali-date' into staging

This commit is contained in:
f 2021-05-14 17:22:34 -03:00
commit 8b9fddc171

View file

@ -15,13 +15,47 @@ class MetadataDocumentDate < MetadataTemplate
true && !private?
end
# Siempre es obligatorio
def required
true
end
def validate
super
errors << I18n.t('metadata.date.invalid_format') unless valid_format?
errors.empty?
end
# El valor puede ser un Date, Time o una String en el formato
# "yyyy-mm-dd"
#
# XXX: Date.iso8601 acepta fechas en el futuro lejano, como 20000,
# pero Jekyll las limita a cuatro cifras, así que vamos a mantener
# eso.
#
# @see {https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb#L15}
def value
return (self[:value] = value_from_document || default_value) if self[:value].nil?
self[:value] =
case self[:value]
when String
begin
Date.iso8601(self[:value]).to_time
rescue Date::Error
value_from_document || default_value
end
else
value_from_document || default_value
end
end
self[:value] = Date.iso8601(self[:value]).to_time if self[:value].is_a? String
private
self[:value]
def valid_format?
return true if self[:value].is_a?(Time)
@valid_format_re ||= /\A\d{2,4}-\d{1,2}-\d{1,2}\z/
@valid_format_re =~ self[:value].to_s
end
end