module ActiveModel::AttributeMethods::ClassMethods
def attribute_method_affix(*affixes)
person.reset_name_to_default!
person.name # => 'Gem'
person = Person.new
end
end
send("#{attr}=", 'Default Name')
def reset_attribute_to_default!(attr)
private
define_attribute_methods :name
attribute_method_affix prefix: 'reset_', suffix: '_to_default!'
attr_accessor :name
include ActiveModel::AttributeMethods
class Person
accept at least the +attr+ argument.
An #{prefix}attribute#{suffix} instance method must exist and
#{prefix}attribute#{suffix}(#{attr}, *args, &block)
to
#{prefix}#{attr}#{suffix}(*args, &block)
the method.
and suffix. Uses +method_missing+ and respond_to? to rewrite
Declares a method available for all attributes with the given prefix
def attribute_method_affix(*affixes) self.attribute_method_matchers += affixes.map! { |affix| AttributeMethodMatcher.new(**affix) } undefine_attribute_methods end