Method: ActiveModel::AttributeMethods::ClassMethods#define_attribute_methods

Defined in:
lib/active_model/attribute_methods.rb

#define_attribute_methods(*attr_names) ⇒ Object

Declares the attributes that should be prefixed and suffixed by ActiveModel::AttributeMethods.

To use, pass attribute names (as strings or symbols), be sure to declare define_attribute_methods after you define any prefix, suffix or affix methods, or they will not hook in.

class Person
  include ActiveModel::AttributeMethods

  attr_accessor :name, :age, :address
  attribute_method_prefix 'clear_'

  # Call to define_attribute_methods must appear after the
  # attribute_method_prefix, attribute_method_suffix or
  # attribute_method_affix declares.
  define_attribute_methods :name, :age, :address

  private

  def clear_attribute(attr)
    send("#{attr}=", nil)
  end
end


251
252
253
# File 'lib/active_model/attribute_methods.rb', line 251

def define_attribute_methods(*attr_names)
  attr_names.flatten.each { |attr_name| define_attribute_method(attr_name) }
end