提交 cb67d166 编写于 作者: J Jeremy Kemper

Cheaper attribute_method_matchers

上级 4b7b8d9e
......@@ -56,6 +56,11 @@ class MissingAttributeError < NoMethodError
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 @@ def #{name}; #{value.to_s.inspect}; end
# 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 @@ def attribute_method_prefix(*prefixes)
# 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 @@ def attribute_method_suffix(*suffixes)
# 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 @@ def method_missing_target
:"#{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 @@ def attribute_method?(attr_name)
# 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 @@ def match_attribute_method?(method_name)
# 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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册