提交 69e84e10 编写于 作者: A Aaron Patterson

return early if the cast attribute has been cached

上级 3f97a97f
......@@ -119,19 +119,24 @@ 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,
# "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
def read_attribute(attr_name)
column = @columns_hash.fetch(attr_name) {
return self.class.type_cast_attribute(attr_name, @attributes, @attributes_cache)
}
# If it's cached, just return it
@attributes_cache.fetch(attr_name) {
value = @attributes.fetch(attr_name) {
return block_given? ? yield(attr_name) : nil
}
column = @columns_hash.fetch(attr_name) {
return self.class.type_cast_attribute(attr_name, @attributes, @attributes_cache)
}
if self.class.cache_attribute?(attr_name)
@attributes_cache[attr_name] ||= column.type_cast(value)
else
column.type_cast value
end
value = @attributes.fetch(attr_name) {
return block_given? ? yield(attr_name) : nil
}
if self.class.cache_attribute?(attr_name)
@attributes_cache[attr_name] ||= column.type_cast(value)
else
column.type_cast value
end
}
end
private
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册