method
define_attr_method
v3.1.0 -
Show latest stable
-
0 notes -
Class: ActiveModel::AttributeMethods::ClassMethods
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0 (0)
- 3.0.9 (-2)
- 3.1.0 (0)
- 3.2.1 (-38)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
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'