Skip to content

Commit

Permalink
Refactor attribute method matcher to use Hash#fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosantoniodasilva committed Jun 27, 2012
1 parent 7fad77f commit 4a20fcf
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions activemodel/lib/active_model/attribute_methods.rb
Expand Up @@ -335,15 +335,13 @@ def attribute_method_matchers_cache #:nodoc:
end end


def attribute_method_matcher(method_name) #:nodoc: def attribute_method_matcher(method_name) #:nodoc:
if attribute_method_matchers_cache.key?(method_name) attribute_method_matchers_cache.fetch(method_name) do |name|
attribute_method_matchers_cache[method_name]
else
# Must try to match prefixes/suffixes first, or else the matcher with no prefix/suffix # Must try to match prefixes/suffixes first, or else the matcher with no prefix/suffix
# will match every time. # will match every time.
matchers = attribute_method_matchers.partition(&:plain?).reverse.flatten(1) matchers = attribute_method_matchers.partition(&:plain?).reverse.flatten(1)
match = nil match = nil
matchers.detect { |method| match = method.match(method_name) } matchers.detect { |method| match = method.match(name) }
attribute_method_matchers_cache[method_name] = match attribute_method_matchers_cache[name] = match
end end
end end


Expand Down

0 comments on commit 4a20fcf

Please sign in to comment.