Permalink
Browse files

Avoid to call send(store_attribute) twice.

  • Loading branch information...
1 parent 9b7401b commit 088022ef262cc43318651cf7bd68e6eb4b5475f6 @kennyj kennyj 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 088022e

Please sign in to comment.