mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-23 00:26:22 +00:00
refactor: agregar definiciones por defecto al post
This commit is contained in:
parent
8c6a70af2b
commit
b4467e4d56
1 changed files with 20 additions and 39 deletions
|
@ -13,8 +13,19 @@ class Post
|
||||||
# Otros atributos que no vienen en los metadatos
|
# Otros atributos que no vienen en los metadatos
|
||||||
PRIVATE_ATTRIBUTES = %i[path slug attributes errors].freeze
|
PRIVATE_ATTRIBUTES = %i[path slug attributes errors].freeze
|
||||||
PUBLIC_ATTRIBUTES = %i[lang date uuid created_at].freeze
|
PUBLIC_ATTRIBUTES = %i[lang date uuid created_at].freeze
|
||||||
|
ALIASED_ATTRIBUTES = %i[locale].freeze
|
||||||
ATTR_SUFFIXES = %w[? =].freeze
|
ATTR_SUFFIXES = %w[? =].freeze
|
||||||
|
|
||||||
|
ATTRIBUTE_DEFINITIONS = {
|
||||||
|
'lang' => { 'type' => 'lang', 'required' => true },
|
||||||
|
'date' => { 'type' => 'document_date', 'required' => true },
|
||||||
|
'uuid' => { 'type' => 'uuid', 'required' => true },
|
||||||
|
'created_at' => { 'type' => 'created_at', 'required' => true },
|
||||||
|
'slug' => { 'type' => 'slug', 'required' => true },
|
||||||
|
'path' => { 'type' => 'path', 'required' => true },
|
||||||
|
'locale' => { 'alias' => 'lang' },
|
||||||
|
}
|
||||||
|
|
||||||
class PostError < StandardError; end
|
class PostError < StandardError; end
|
||||||
class UnknownAttributeError < PostError; end
|
class UnknownAttributeError < PostError; end
|
||||||
|
|
||||||
|
@ -49,10 +60,12 @@ class Post
|
||||||
@layout = args[:layout]
|
@layout = args[:layout]
|
||||||
@site = args[:site]
|
@site = args[:site]
|
||||||
@document = args[:document]
|
@document = args[:document]
|
||||||
@attributes = layout.attributes + PUBLIC_ATTRIBUTES
|
@attributes = (layout.attributes + PUBLIC_ATTRIBUTES).uniq
|
||||||
@errors = {}
|
@errors = {}
|
||||||
@metadata = {}
|
@metadata = {}
|
||||||
|
|
||||||
|
layout.metadata = ATTRIBUTE_DEFINITIONS.merge(layout.metadata).with_indifferent_access
|
||||||
|
|
||||||
# Leer el documento si existe
|
# Leer el documento si existe
|
||||||
# @todo Asignar todos los valores a self[:value] luego de leer
|
# @todo Asignar todos los valores a self[:value] luego de leer
|
||||||
document&.read! unless new?
|
document&.read! unless new?
|
||||||
|
@ -195,6 +208,10 @@ class Post
|
||||||
define_singleton_method(name) do
|
define_singleton_method(name) do
|
||||||
template = layout.metadata[name.to_s]
|
template = layout.metadata[name.to_s]
|
||||||
|
|
||||||
|
if template.key?('alias')
|
||||||
|
return public_send(template['alias'].to_sym)
|
||||||
|
end
|
||||||
|
|
||||||
@metadata[name] ||=
|
@metadata[name] ||=
|
||||||
MetadataFactory.build(document: document,
|
MetadataFactory.build(document: document,
|
||||||
post: self,
|
post: self,
|
||||||
|
@ -210,43 +227,6 @@ class Post
|
||||||
public_send name
|
public_send name
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: Mover a method_missing
|
|
||||||
def slug
|
|
||||||
@metadata[:slug] ||= MetadataSlug.new(document: document, site: site, layout: layout, name: :slug, type: :slug,
|
|
||||||
post: self, required: true)
|
|
||||||
end
|
|
||||||
|
|
||||||
# TODO: Mover a method_missing
|
|
||||||
def date
|
|
||||||
@metadata[:date] ||= MetadataDocumentDate.new(document: document, site: site, layout: layout, name: :date,
|
|
||||||
type: :document_date, post: self, required: true)
|
|
||||||
end
|
|
||||||
|
|
||||||
# TODO: Mover a method_missing
|
|
||||||
def path
|
|
||||||
@metadata[:path] ||= MetadataPath.new(document: document, site: site, layout: layout, name: :path, type: :path,
|
|
||||||
post: self, required: true)
|
|
||||||
end
|
|
||||||
|
|
||||||
# TODO: Mover a method_missing
|
|
||||||
def lang
|
|
||||||
@metadata[:lang] ||= MetadataLang.new(document: document, site: site, layout: layout, name: :lang, type: :lang,
|
|
||||||
post: self, required: true)
|
|
||||||
end
|
|
||||||
|
|
||||||
alias locale lang
|
|
||||||
|
|
||||||
# TODO: Mover a method_missing
|
|
||||||
def uuid
|
|
||||||
@metadata[:uuid] ||= MetadataUuid.new(document: document, site: site, layout: layout, name: :uuid, type: :uuid,
|
|
||||||
post: self, required: true)
|
|
||||||
end
|
|
||||||
|
|
||||||
# La fecha de creación inmodificable del post
|
|
||||||
def created_at
|
|
||||||
@metadata[:created_at] ||= MetadataCreatedAt.new(document: document, site: site, layout: layout, name: :created_at, type: :created_at, post: self, required: true)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Devuelve los strong params para el layout.
|
# Devuelve los strong params para el layout.
|
||||||
#
|
#
|
||||||
# XXX: Nos gustaría no tener que instanciar Metadata acá, pero depende
|
# XXX: Nos gustaría no tener que instanciar Metadata acá, pero depende
|
||||||
|
@ -423,7 +403,8 @@ class Post
|
||||||
def attribute?(mid)
|
def attribute?(mid)
|
||||||
included = DEFAULT_ATTRIBUTES.include?(mid) ||
|
included = DEFAULT_ATTRIBUTES.include?(mid) ||
|
||||||
PRIVATE_ATTRIBUTES.include?(mid) ||
|
PRIVATE_ATTRIBUTES.include?(mid) ||
|
||||||
PUBLIC_ATTRIBUTES.include?(mid)
|
PUBLIC_ATTRIBUTES.include?(mid) ||
|
||||||
|
ALIASED_ATTRIBUTES.include?(mid)
|
||||||
|
|
||||||
included = attributes.include? mid if !included && singleton_class.method_defined?(:attributes)
|
included = attributes.include? mid if !included && singleton_class.method_defined?(:attributes)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue