method

define_attr_method

Importance_2
v3.1.0 - Show latest stable - 0 notes - Class: ActiveModel::AttributeMethods::ClassMethods
define_attr_method(name, value=nil, &block) public

Defines an “attribute” method (like inheritance_column or table_name). A new (class) method will be created with the given name. If a value is specified, the new method will return that value (as a string). Otherwise, the given block will be used to compute the value of the method.

The original method will be aliased, with the new name being prefixed with “original_”. This allows the new method to access the original value.

Example:

class Person

  include ActiveModel::AttributeMethods

  cattr_accessor :primary_key
  cattr_accessor :inheritance_column

  define_attr_method :primary_key, "sysid"
  define_attr_method( :inheritance_column ) do
    original_inheritance_column + "_id"
  end

end

Provides you with:

AttributePerson.primary_key
# => "sysid"
AttributePerson.inheritance_column = 'address'
AttributePerson.inheritance_column
# => 'address_id'
Show source
Register or log in to add new notes.