Method: Module#alias_attribute
- Defined in:
- lib/core_ext.rb
#alias_attribute(new_name, old_name) ⇒ Object
Allows you to make aliases for attributes, which includes getter, setter, and query methods.
Example:
class Content < ActiveRecord::Base
# has a title attribute
end
class Email < Content
alias_attribute :subject, :title
end
e = Email.find(1)
e.title # => "Superstars"
e.subject # => "Superstars"
e.subject? # => true
e.subject = "Megastars"
e.title # => "Megastars"
201 202 203 204 205 206 207 |
# File 'lib/core_ext.rb', line 201 def alias_attribute(new_name, old_name) module_eval " def \#{new_name}; self.\#{old_name}; end # def subject; self.title; end\n def \#{new_name}?; self.\#{old_name}?; end # def subject?; self.title?; end\n def \#{new_name}=(v); self.\#{old_name} = v; end # def subject=(v); self.title = v; end\n STR\nend\n", __FILE__, __LINE__ + 1 |