class method self.define_attr_method

Ruby on Rails 2.2.3

Since v2.2.3 Last seen in v2.3.18 Private

Available in: v2.2.3 v2.3.18

Signature

self.define_attr_method(name, value=nil, &block)

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 A < ActiveRecord::Base
  define_attr_method :primary_key, "sysid"
  define_attr_method( :inheritance_column ) do
    original_inheritance_column + "_id"
  end
end

Parameters

name req
value opt = nil
block block
Source
# File activerecord/lib/active_record/base.rb, line 1901
        def define_attr_method(name, value=nil, &block)
          sing = class << self; self; end
          sing.send :alias_method, "original_#{name}", name
          if block_given?
            sing.send :define_method, name, &block
          else
            # use eval instead of a block to work around a memory leak in dev
            # mode in fcgi
            sing.class_eval "def #{name}; #{value.to_s.inspect}; end"
          end
        end

Defined in activerecord/lib/active_record/base.rb line 1901 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::Base

Type at least 2 characters to search.

↑↓ navigate · open · esc close