提交 8cbad029 编写于 作者: J Jon Leighton

Optimize for the happy path

Checking respond_to? incurs overhead, and most of the time when
assigning attributes it will return true. So just handle the
NoMethodError instead.
上级 2ff47c48
......@@ -97,22 +97,15 @@ def assign_attributes(new_attributes, options = {})
attributes.each do |k, v|
if k.include?("(")
multi_parameter_attributes << [ k, v ]
elsif respond_to?("#{k}=")
if v.is_a?(Hash)
nested_parameter_attributes << [ k, v ]
else
send("#{k}=", v)
end
elsif v.is_a?(Hash)
nested_parameter_attributes << [ k, v ]
else
raise(UnknownAttributeError, "unknown attribute: #{k}")
_assign_attribute(k, v)
end
end
# assign any deferred nested attributes after the base attributes have been set
nested_parameter_attributes.each do |k,v|
send("#{k}=", v)
end
nested_parameter_attributes.each { |k,v| _assign_attribute(k, v) }
assign_multiparameter_attributes(multi_parameter_attributes)
ensure
@mass_assignment_options = previous_options
......@@ -130,6 +123,16 @@ def mass_assignment_role
private
def _assign_attribute(k, v)
public_send("#{k}=", v)
rescue NoMethodError
if respond_to?("#{k}=")
raise
else
raise UnknownAttributeError, "unknown attribute: #{k}"
end
end
# Instantiates objects for all attribute classes that needs more than one constructor parameter. This is done
# by calling new on the column type or aggregation type (through composed_of) object with these parameters.
# So having the pairs written_on(1) = "2004", written_on(2) = "6", written_on(3) = "24", will instantiate
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册