Skip to content

Commit

Permalink
moved most of the evald code in to regular ruby code
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Feb 7, 2012
1 parent 422cc57 commit ee161d1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
38 changes: 28 additions & 10 deletions activerecord/lib/active_record/attribute_methods/read.rb
Expand Up @@ -92,17 +92,13 @@ def cacheable_column?(column)
end

def internal_attribute_access_code(attr_name, cast_code)
cast_code = instance_cast_code(attr_name)

access_code = "v = @attributes.fetch(attr_name) { missing_attribute(attr_name, caller) };"

access_code << "v && #{cast_code};"
method = instance_cast_method(attr_name)

if cache_attribute?(attr_name)
access_code = "@attributes_cache[attr_name] ||= (#{access_code})"
"cached_cast_attribute('#{attr_name}', :#{method})"
else
"cast_attribute('#{attr_name}', :#{method})"
end

"attr_name = '#{attr_name}'; #{access_code}"
end

def external_attribute_access_code(attr_name, cast_code)
Expand All @@ -119,8 +115,8 @@ def attribute_cast_code(attr_name)
columns_hash[attr_name].type_cast_code('v')
end

def instance_cast_code(attr_name)
"@columns_hash[attr_name].type_cast(v)"
def instance_cast_method(attr_name)
"cast_column"
end
end

Expand All @@ -132,6 +128,28 @@ def read_attribute(attr_name)


private
def cached_cast_attribute(attr_name, method)
@attributes_cache[attr_name] ||= cast_attribute(attr_name, method)
end

def cast_attribute(attr_name, method)
v = @attributes.fetch(attr_name) { missing_attribute(attr_name, caller) }
v && send(method, attr_name, v)
end

def cast_serialized(attr_name, value)
value.unserialized_value
end

def cast_tz_conversion(attr_name, value)
value = cast_column(attr_name, value)
value.acts_like?(:time) ? value.in_time_zone : value
end

def cast_column(attr_name, value)
@columns_hash[attr_name].type_cast value
end

def attribute(attribute_name)
read_attribute(attribute_name)
end
Expand Down
Expand Up @@ -80,9 +80,9 @@ def attribute_cast_code(attr_name)
end
end

def instance_cast_code(attr_name)
def instance_cast_method(attr_name)
if serialized_attributes.include?(attr_name)
"v.unserialized_value"
"cast_serialized"
else
super
end
Expand Down
Expand Up @@ -53,14 +53,11 @@ def #{attr_name}=(original_time)
end

private
def instance_cast_code(attr_name)
def instance_cast_method(attr_name)
column = columns_hash[attr_name]

if create_time_zone_conversion_attribute?(attr_name, column)
typecast = "v = #{super}"
time_zone_conversion = "v.acts_like?(:time) ? v.in_time_zone : v"

"((#{typecast}) && (#{time_zone_conversion}))"
"cast_tz_conversion"
else
super
end
Expand Down

0 comments on commit ee161d1

Please sign in to comment.