module ActiveRecord::AttributeMethods::Read::ClassMethods

def cache_attribute?(attr_name)

Returns +true+ if the provided attribute is being cached.
def cache_attribute?(attr_name)
  cached_attributes.include?(attr_name)
end

def cache_attributes(*attribute_names)

+created_at+, +updated_at+).
with expensive conversion methods, like time related columns (e.g.
values should be cached. Usually caching only pays off for attributes
+cache_attributes+ allows you to declare which converted attribute
def cache_attributes(*attribute_names)
  cached_attributes.merge attribute_names.map { |attr| attr.to_s }
end

def cacheable_column?(column)

def cacheable_column?(column)
  if attribute_types_cached_by_default == ATTRIBUTE_TYPES_CACHED_BY_DEFAULT
    ! serialized_attributes.include? column.name
  else
    attribute_types_cached_by_default.include?(column.type)
  end
end

def cached_attributes

with datatype :datetime, :timestamp, :time, :date are cached.
Returns the attributes which are cached. By default time related columns
def cached_attributes
  @cached_attributes ||= columns.select { |c| cacheable_column?(c) }.map { |col| col.name }.to_set
end

def define_method_attribute(name)

key the @attributes_cache in read_attribute.
Making it frozen means that it doesn't get duped when used to
to allocate an object on each call to the attribute method.
the attribute name. Using a constant means that we do not have
We are also defining a constant to hold the frozen string of

it to what we want.
the __temp__ identifier, and then use alias method to rename
'my_column(omg)'. So to work around this we first define with
characters that are not allowed in normal method names (like
But sometimes the database might return columns with

created, then define_method will consume much less memory.
be slower on dispatch, but if you're careful about the closure
sequences are duplicated and cached (in MRI). define_method may
Evaluating many similar methods may use more memory as the instruction
define_method, because define_method is slower on dispatch.
We want to generate the methods via module_eval rather than
def define_method_attribute(name)
  safe_name = name.unpack('h*').first
  generated_attribute_methods::AttrNames.set_name_cache safe_name, name
  generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
    def __temp__#{safe_name}
      read_attribute(AttrNames::ATTR_#{safe_name}) { |n| missing_attribute(n, caller) }
    end
    alias_method #{name.inspect}, :__temp__#{safe_name}
    undef_method :__temp__#{safe_name}
  STR
end