@@ -32,36 +32,21 @@ def cache_attribute?(attr_name)
3232
3333 protected
3434
35- # We want to generate the methods via module_eval rather than
36- # define_method, because define_method is slower on dispatch and
37- # uses more memory (because it creates a closure).
35+ # We want to generate the methods via module_eval rather than define_method,
36+ # because define_method is slower on dispatch and uses more memory (because it
37+ # creates a closure).
3838 #
39- # But sometimes the database might return columns with
40- # characters that are not allowed in normal method names (like
41- # 'my_column(omg)'. So to work around this we first define with
42- # the __temp__ identifier, and then use alias method to rename
43- # it to what we want.
44- #
45- # We are also defining a constant to hold the frozen string of
46- # the attribute name. Using a constant means that we do not have
47- # to allocate an object on each call to the attribute method.
48- # Making it frozen means that it doesn't get duped when used to
49- # key the @attributes_cache in read_attribute.
50- def define_method_attribute ( name )
51- safe_name = name . unpack ( 'h*' ) . first
39+ # But sometimes the database might return columns with characters that are not
40+ # allowed in normal method names (like 'my_column(omg)'. So to work around this
41+ # we first define with the __temp__ identifier, and then use alias method to
42+ # rename it to what we want.
43+ def define_method_attribute ( attr_name )
5244 generated_attribute_methods . module_eval <<-STR , __FILE__ , __LINE__ + 1
53- module AttrNames
54- unless defined? ATTR_#{ safe_name }
55- ATTR_#{ safe_name } = #{ name . inspect } .freeze
56- end
45+ def __temp__
46+ read_attribute('#{ attr_name } ') { |n| missing_attribute(n, caller) }
5747 end
58-
59- def __temp__#{ safe_name }
60- read_attribute(AttrNames::ATTR_#{ safe_name } ) { |n| missing_attribute(n, caller) }
61- end
62-
63- alias_method #{ name . inspect } , :__temp__#{ safe_name }
64- undef_method :__temp__#{ safe_name }
48+ alias_method '#{ attr_name } ', :__temp__
49+ undef_method :__temp__
6550 STR
6651 end
6752
0 commit comments