diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2010-08-30 23:56:23 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2010-09-09 14:37:58 -0700 |
commit | cb67d166297ff6745358b804c61277c902e87023 (patch) | |
tree | acefad2f412e7b28963d9ef961945a7590edc604 /activemodel | |
parent | 4b7b8d9e19b662d8a1135fec73b422202f97472a (diff) | |
download | rails-cb67d166297ff6745358b804c61277c902e87023.tar.gz rails-cb67d166297ff6745358b804c61277c902e87023.tar.bz2 rails-cb67d166297ff6745358b804c61277c902e87023.zip |
Cheaper attribute_method_matchers
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/lib/active_model/attribute_methods.rb | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index a5de5ce325..149f6abf61 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -56,6 +56,11 @@ module ActiveModel module AttributeMethods extend ActiveSupport::Concern + included do + class_attribute :attribute_method_matchers, :instance_writer => false + self.attribute_method_matchers = [] + end + module ClassMethods # Defines an "attribute" method (like +inheritance_column+ or +table_name+). # A new (class) method will be created with the given name. If a value is @@ -143,7 +148,7 @@ module ActiveModel # person.clear_name # person.name # => nil def attribute_method_prefix(*prefixes) - attribute_method_matchers.concat(prefixes.map { |prefix| AttributeMethodMatcher.new :prefix => prefix }) + self.attribute_method_matchers += prefixes.map { |prefix| AttributeMethodMatcher.new :prefix => prefix } undefine_attribute_methods end @@ -180,7 +185,7 @@ module ActiveModel # person.name # => "Bob" # person.name_short? # => true def attribute_method_suffix(*suffixes) - attribute_method_matchers.concat(suffixes.map { |suffix| AttributeMethodMatcher.new :suffix => suffix }) + self.attribute_method_matchers += suffixes.map { |suffix| AttributeMethodMatcher.new :suffix => suffix } undefine_attribute_methods end @@ -218,7 +223,7 @@ module ActiveModel # person.reset_name_to_default! # person.name # => 'Gemma' def attribute_method_affix(*affixes) - attribute_method_matchers.concat(affixes.map { |affix| AttributeMethodMatcher.new :prefix => affix[:prefix], :suffix => affix[:suffix] }) + self.attribute_method_matchers += affixes.map { |affix| AttributeMethodMatcher.new :prefix => affix[:prefix], :suffix => affix[:suffix] } undefine_attribute_methods end @@ -338,10 +343,6 @@ module ActiveModel :"#{prefix}attribute#{suffix}" end end - - def attribute_method_matchers #:nodoc: - read_inheritable_attribute(:attribute_method_matchers) || write_inheritable_attribute(:attribute_method_matchers, []) - end end # Allows access to the object attributes, which are held in the @@ -390,7 +391,7 @@ module ActiveModel # Returns a struct representing the matching attribute method. # The struct's attributes are prefix, base and suffix. def match_attribute_method?(method_name) - self.class.send(:attribute_method_matchers).each do |method| + self.class.attribute_method_matchers.each do |method| if (match = method.match(method_name)) && attribute_method?(match.attr_name) return match end @@ -401,7 +402,7 @@ module ActiveModel # prevent method_missing from calling private methods with #send def guard_private_attribute_method!(method_name, args) if self.class.private_method_defined?(method_name) - raise NoMethodError.new("Attempt to call private method", method_name, args) + raise NoMethodError.new("Attempt to call private method `#{method_name}'", method_name, args) end end |