module ActiveModel::AttributeMethods::ClassMethods
def define_attr_method(name, value=nil, &block)
AttributePerson.inheritance_column
AttributePerson.inheritance_column = 'address'
# => "sysid"
AttributePerson.primary_key
Provides you with:
end
end
original_inheritance_column + "_id"
define_attr_method( :inheritance_column ) do
define_attr_method :primary_key, "sysid"
cattr_accessor :inheritance_column
cattr_accessor :primary_key
include ActiveModel::AttributeMethods
class Person
Example:
value.
with "original_". This allows the new method to access the original
The original method will be aliased, with the new name being prefixed
method.
Otherwise, the given block will be used to compute the value of the
specified, the new method will return that value (as a string).
A new (class) method will be created with the given name. If a value is
Defines an "attribute" method (like +inheritance_column+ or +table_name+).
def define_attr_method(name, value=nil, &block) sing = singleton_class sing.class_eval <<-eorb, __FILE__, __LINE__ + 1 if method_defined?(:'original_#{name}') undef :'original_#{name}' end alias_method :'original_#{name}', :'#{name}' eorb if block_given? sing.send :define_method, name, &block else # If we can compile the method name, do it. Otherwise use define_method. # This is an important *optimization*, please don't change it. define_method # has slower dispatch and consumes more memory. if name =~ COMPILABLE_REGEXP sing.class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{name}; #{value.nil? ? 'nil' : value.to_s.inspect}; end RUBY else value = value.to_s if value sing.send(:define_method, name) { value } end end end