Module: ActiveRecord::AttributeMethods::Read

Extended by:
ActiveSupport::Concern
Defined in:
activerecord/lib/active_record/attribute_methods/read.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

ReaderMethodCache =
Class.new(AttributeMethodCache) {
  private  # We want to generate the methods via module_eval rather than
  # define_method, because define_method is slower on dispatch.
  # Evaluating many similar methods may use more memory as the instruction
  # sequences are duplicated and cached (in MRI).  define_method may
  # be slower on dispatch, but if you're careful about the closure
  # created, then define_method will consume much less memory.
  #
  # But sometimes the database might return columns with
  # characters that are not allowed in normal method names (like
  # 'my_column(omg)'. So to work around this we first define with
  # the __temp__ identifier, and then use alias method to rename
  # it to what we want.
  #
  # We are also defining a constant to hold the frozen string of
  # the attribute name. Using a constant means that we do not have
  # to allocate an object on each call to the attribute method.
  # Making it frozen means that it doesn't get duped when used to
  # key the @attributes in read_attribute.

  def method_body(method_name, const_name)
    <<-EOMETHOD
    def #{method_name}
      name = ::ActiveRecord::AttributeMethods::AttrNames::ATTR_#{const_name}
      _read_attribute(name) { |n| missing_attribute(n, caller) }
    end
    EOMETHOD
  end
}.new
ID =
'id'.freeze

Instance Method Summary collapse

Methods included from ActiveSupport::Concern

append_features, class_methods, extended, included

Instance Method Details

#_read_attribute(attr_name) ⇒ Object

This method exists to avoid the expensive primary_key check internally, without breaking compatibility with the read_attribute API


92
93
94
# File 'activerecord/lib/active_record/attribute_methods/read.rb', line 92

def _read_attribute(attr_name) # :nodoc:
  @attributes.fetch_value(attr_name.to_s) { |n| yield n if block_given? }
end

#read_attribute(attr_name, &block) ⇒ Object

Returns the value of the attribute identified by attr_name after it has been typecast (for example, “2004-12-12” in a date column is cast to a date object, like Date.new(2004, 12, 12)).


84
85
86
87
88
# File 'activerecord/lib/active_record/attribute_methods/read.rb', line 84

def read_attribute(attr_name, &block)
  name = attr_name.to_s
  name = self.class.primary_key if name == ID
  _read_attribute(name, &block)
end