optimizacion: usar menos Post#attribute_name

This commit is contained in:
f 2020-01-02 20:29:53 -03:00
parent 5c8d8aafdf
commit a3effa6e26
No known key found for this signature in database
GPG key ID: 2AE5A13E321F953D

View file

@ -89,14 +89,18 @@ class Post < OpenStruct
# XXX: rubocop dice que tenemos que usar super cuando ya lo estamos
# usando...
def method_missing(mid, *args)
unless attribute? mid
# Limpiar el nombre del atributo, para que todos los ayudantes
# reciban el método en limpio
name = attribute_name mid
unless attribute? name
raise NoMethodError, I18n.t('exceptions.post.no_method',
method: mid)
end
# Definir los attribute_*
new_attribute_was(mid)
new_attribute_changed(mid)
new_attribute_was(name)
new_attribute_changed(name)
# OpenStruct
super(mid, *args)
@ -108,13 +112,12 @@ class Post < OpenStruct
# Detecta si es un atributo válido o no, a partir de la tabla de la
# plantilla
def attribute?(mid)
mid = attribute_name mid
included = DEFAULT_ATTRIBUTES.include?(mid) ||
PRIVATE_ATTRIBUTES.include?(mid) ||
PUBLIC_ATTRIBUTES.include?(mid)
if !included && singleton_class.method_defined?(:attributes)
included = attributes.include? attribute_name(mid)
included = attributes.include? mid
end
included
@ -275,7 +278,7 @@ class Post < OpenStruct
end
def new_attribute_was(method)
attr_was = (attribute_name(method).to_s + '_was').to_sym
attr_was = "#{method}_was".to_sym
return attr_was if singleton_class.method_defined? attr_was
define_singleton_method(attr_was) do
@ -290,7 +293,7 @@ class Post < OpenStruct
# Pregunta si el atributo cambió
def new_attribute_changed(method)
attr_changed = (attribute_name(method).to_s + '_changed?').to_sym
attr_changed = "#{method}_changed?".to_sym
return attr_changed if singleton_class.method_defined? attr_changed