Returns true if the provided attribute is being cached.
# File lib/active_record/attribute_methods/read.rb, line 34
34: def cache_attribute?(attr_name)
35: cached_attributes.include?(attr_name)
36: end
cache_attributes allows you to declare which converted attribute values should be cached. Usually caching only pays off for attributes with expensive conversion methods, like time related columns (e.g. created_at, updated_at).
# File lib/active_record/attribute_methods/read.rb, line 22
22: def cache_attributes(*attribute_names)
23: attribute_names.each {|attr| cached_attributes << attr.to_s}
24: end
Returns the attributes which are cached. By default time related columns with datatype :datetime, :timestamp, :time, :date are cached.
# File lib/active_record/attribute_methods/read.rb, line 28
28: def cached_attributes
29: @cached_attributes ||=
30: columns.select{|c| attribute_types_cached_by_default.include?(c.type)}.map{|col| col.name}.to_set
31: end
# File lib/active_record/attribute_methods/read.rb, line 39
39: def define_method_attribute(attr_name)
40: if self.serialized_attributes[attr_name]
41: define_read_method_for_serialized_attribute(attr_name)
42: else
43: define_read_method(attr_name.to_sym, attr_name, columns_hash[attr_name])
44: end
45:
46: if attr_name == primary_key && attr_name != "id"
47: define_read_method(:id, attr_name, columns_hash[attr_name])
48: end
49: end
Define an attribute reader method. Cope with nil column.
# File lib/active_record/attribute_methods/read.rb, line 58
58: def define_read_method(symbol, attr_name, column)
59: cast_code = column.type_cast_code('v') if column
60: access_code = cast_code ? "(v=@attributes['#{attr_name}']) && #{cast_code}" : "@attributes['#{attr_name}']"
61:
62: unless attr_name.to_s == self.primary_key.to_s
63: access_code = access_code.insert(0, "missing_attribute('#{attr_name}', caller) unless @attributes.has_key?('#{attr_name}'); ")
64: end
65:
66: if cache_attribute?(attr_name)
67: access_code = "@attributes_cache['#{attr_name}'] ||= (#{access_code})"
68: end
69: generated_attribute_methods.module_eval("def #{symbol}; #{access_code}; end", __FILE__, __LINE__)
70: end
Define read method for serialized attribute.
# File lib/active_record/attribute_methods/read.rb, line 53
53: def define_read_method_for_serialized_attribute(attr_name)
54: generated_attribute_methods.module_eval("def #{attr_name}; unserialize_attribute('#{attr_name}'); end", __FILE__, __LINE__)
55: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.