Permalink
Browse files

Merge pull request #7056 from kennyj/avoid_twice_call

Avoid to call send(store_attribute) twice.
  • Loading branch information...
2 parents 9b7401b + 088022e commit b7ad71ba12ed8bf94b0f00584e6dc04a57795d53 @rafaelfranca rafaelfranca committed Jul 14, 2012
Showing with 8 additions and 6 deletions.
  1. +8 −6 activerecord/lib/active_record/store.rb
@@ -57,17 +57,15 @@ def store_accessor(store_attribute, *keys)
keys = keys.flatten
keys.each do |key|
define_method("#{key}=") do |value|
- initialize_store_attribute(store_attribute)
- attribute = send(store_attribute)
+ attribute = initialize_store_attribute(store_attribute)
if value != attribute[key]
attribute[key] = value
send :"#{store_attribute}_will_change!"
end
end
define_method(key) do
- initialize_store_attribute(store_attribute)
- send(store_attribute)[key]
+ initialize_store_attribute(store_attribute)[key]
end
end
@@ -77,8 +75,12 @@ def store_accessor(store_attribute, *keys)
private
def initialize_store_attribute(store_attribute)
- attr = send(store_attribute)
- send :"#{store_attribute}=", IndifferentCoder.as_indifferent_hash(attr) unless attr.is_a?(HashWithIndifferentAccess)
+ attribute = send(store_attribute)
+ unless attribute.is_a?(HashWithIndifferentAccess)
+ attribute = IndifferentCoder.as_indifferent_hash(attribute)
+ send :"#{store_attribute}=", attribute
+ end
+ attribute
end
class IndifferentCoder

0 comments on commit b7ad71b

Please sign in to comment.