Skip to main content

attribute_query_methods

🏗️ Dynamic attribute query generators

Metaprogram dynamic predicate methods (dirty?, present?, etc.) for model attributes, reducing boilerplate. Use define_method in a loop and handle boolean suffixes automatically.

class Resource
ATTRS = %i[active archived]

ATTRS.each do |attr|
define_method("#{attr}?") { get_attribute(attr) }
define_method("#{attr}!") { update_attribute(attr, true) }
end
end