Skip to content

Commit

Permalink
Optimization of ActiveModel's match_attribute_method?
Browse files Browse the repository at this point in the history
  • Loading branch information
lawrencepit committed Jul 14, 2011
1 parent f88e9d8 commit f04c5d6
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions activemodel/lib/active_model/attribute_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -410,13 +410,16 @@ def attribute_method?(attr_name)
private
# Returns a struct representing the matching attribute method.
# The struct's attributes are prefix, base and suffix.
@@match_attribute_method_cache = {}
def match_attribute_method?(method_name)
cache = @@match_attribute_method_cache[self.class] ||= {}
return cache[method_name] if cache.key?(method_name)
self.class.attribute_method_matchers.each do |method|
if (match = method.match(method_name)) && attribute_method?(match.attr_name)
return match
return cache[method_name] = match
end
end
nil
cache[method_name] = nil
end

# prevent method_missing from calling private methods with #send
Expand Down

0 comments on commit f04c5d6

Please sign in to comment.