提交 6aea5135 编写于 作者: A Aaron Patterson

eagerly assign the attribute name cache, remove const_missing

上级 8eb7561a
...@@ -30,8 +30,11 @@ def initialize_generated_modules # :nodoc: ...@@ -30,8 +30,11 @@ def initialize_generated_modules # :nodoc:
extend Mutex_m extend Mutex_m
const_set :AttrNames, Module.new { const_set :AttrNames, Module.new {
def self.const_missing(name) def self.set_name_cache(name, value)
const_set(name, [name.to_s.sub(/ATTR_/, '')].pack('h*').freeze) const_name = "ATTR_#{name}"
unless const_defined? const_name
const_set const_name, value
end
end end
} }
} }
......
...@@ -49,6 +49,8 @@ def cache_attribute?(attr_name) ...@@ -49,6 +49,8 @@ def cache_attribute?(attr_name)
# key the @attributes_cache in read_attribute. # key the @attributes_cache in read_attribute.
def define_method_attribute(name) def define_method_attribute(name)
safe_name = name.unpack('h*').first safe_name = name.unpack('h*').first
generated_attribute_methods::AttrNames.set_name_cache safe_name, name
generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1 generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
def __temp__#{safe_name} def __temp__#{safe_name}
read_attribute(AttrNames::ATTR_#{safe_name}) { |n| missing_attribute(n, caller) } read_attribute(AttrNames::ATTR_#{safe_name}) { |n| missing_attribute(n, caller) }
......
...@@ -14,6 +14,8 @@ module ClassMethods ...@@ -14,6 +14,8 @@ module ClassMethods
# this code. # this code.
def define_method_attribute=(name) def define_method_attribute=(name)
safe_name = name.unpack('h*').first safe_name = name.unpack('h*').first
generated_attribute_methods::AttrNames.set_name_cache safe_name, name
generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1 generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
def __temp__#{safe_name}=(value) def __temp__#{safe_name}=(value)
write_attribute(AttrNames::ATTR_#{safe_name}, value) write_attribute(AttrNames::ATTR_#{safe_name}, value)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册