mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Cheaper attribute_method_matchers
This commit is contained in:
parent
4b7b8d9e19
commit
cb67d16629
1 changed files with 10 additions and 9 deletions
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue