module ActiveModel::AttributeMethods::ClassMethods
def alias_attribute(new_name, old_name)
person.name_short? # => true
person.nickname # => "Bob"
person.name # => "Bob"
person.name = 'Bob'
person = Person.new
end
end
send(attr).length < 5
def attribute_short?(attr)
private
alias_attribute :nickname, :name
define_attribute_methods :name
attribute_method_suffix '_short?'
attr_accessor :name
include ActiveModel::AttributeMethods
class Person
Allows you to make aliases for attributes.
def alias_attribute(new_name, old_name) self.attribute_aliases = attribute_aliases.merge(new_name.to_s => old_name.to_s) ActiveSupport::CodeGenerator.batch(self, __FILE__, __LINE__) do |code_generator| attribute_method_matchers.each do |matcher| method_name = matcher.method_name(new_name).to_s target_name = matcher.method_name(old_name).to_s parameters = matcher.parameters mangled_name = target_name unless NAME_COMPILABLE_REGEXP.match?(target_name) mangled_name = "__temp__#{target_name.unpack1("h*")}" end code_generator.define_cached_method(method_name, as: mangled_name, namespace: :alias_attribute) do |batch| body = if CALL_COMPILABLE_REGEXP.match?(target_name) "self.#{target_name}(#{parameters || ''})" else call_args = [":'#{target_name}'"] call_args << parameters if parameters "send(#{call_args.join(", ")})" end modifier = matcher.parameters == FORWARD_PARAMETERS ? "ruby2_keywords " : "" batch << "#{modifier}def #{mangled_name}(#{parameters || ''})" << body << "end" end end end end