提交 3f97a97f 编写于 作者: A Aaron Patterson

always call `read_attribute` from the reader method

上级 e54d5cc5
...@@ -94,13 +94,7 @@ def cacheable_column?(column) ...@@ -94,13 +94,7 @@ def cacheable_column?(column)
end end
def internal_attribute_access_code(attr_name, cast_code) def internal_attribute_access_code(attr_name, cast_code)
method = instance_cast_method(attr_name) "read_attribute('#{attr_name}') { |n| missing_attribute(n, caller) }"
if cache_attribute?(attr_name)
"cached_cast_attribute('#{attr_name}', :#{method})"
else
"cast_attribute('#{attr_name}', :#{method})"
end
end end
def external_attribute_access_code(attr_name, cast_code) def external_attribute_access_code(attr_name, cast_code)
...@@ -125,14 +119,18 @@ def instance_cast_method(attr_name) ...@@ -125,14 +119,18 @@ def instance_cast_method(attr_name)
# Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example, # Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example,
# "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)). # "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
def read_attribute(attr_name) def read_attribute(attr_name)
if @columns_hash.key? attr_name column = @columns_hash.fetch(attr_name) {
if self.class.cache_attribute?(attr_name) return self.class.type_cast_attribute(attr_name, @attributes, @attributes_cache)
@attributes_cache[attr_name] ||= @columns_hash[attr_name].type_cast(@attributes[attr_name]) }
else
@columns_hash[attr_name].type_cast @attributes[attr_name] value = @attributes.fetch(attr_name) {
end return block_given? ? yield(attr_name) : nil
}
if self.class.cache_attribute?(attr_name)
@attributes_cache[attr_name] ||= column.type_cast(value)
else else
self.class.type_cast_attribute(attr_name, @attributes, @attributes_cache) column.type_cast value
end end
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册