lib/active_model/attribute_methods.rb in activemodel-3.0.5 vs lib/active_model/attribute_methods.rb in activemodel-3.0.6.rc1

- old
+ new

@@ -91,23 +91,26 @@ # AttributePerson.inheritance_column # # => 'address_id' def define_attr_method(name, value=nil, &block) sing = singleton_class sing.class_eval <<-eorb, __FILE__, __LINE__ + 1 - if method_defined?(:original_#{name}) - undef :original_#{name} + if method_defined?(:'original_#{name}') + undef :'original_#{name}' end - alias_method :original_#{name}, :#{name} + alias_method :'original_#{name}', :'#{name}' eorb if block_given? sing.send :define_method, name, &block else - # use eval instead of a block to work around a memory leak in dev - # mode in fcgi - sing.class_eval <<-eorb, __FILE__, __LINE__ + 1 - def #{name}; #{value.to_s.inspect}; end - eorb + if name =~ /^[a-zA-Z_]\w*[!?=]?$/ + sing.class_eval <<-eorb, __FILE__, __LINE__ + 1 + def #{name}; #{value.nil? ? 'nil' : value.to_s.inspect}; end + eorb + else + value = value.to_s if value + sing.send(:define_method, name) { value } + end end end # Declares a method available for all attributes with the given prefix. # Uses +method_missing+ and <tt>respond_to?</tt> to rewrite the method. @@ -224,11 +227,11 @@ def alias_attribute(new_name, old_name) attribute_method_matchers.each do |matcher| module_eval <<-STR, __FILE__, __LINE__ + 1 def #{matcher.method_name(new_name)}(*args) - send(:#{matcher.method_name(old_name)}, *args) + send(:'#{matcher.method_name(old_name)}', *args) end STR end end @@ -267,14 +270,14 @@ send(generate_method, attr_name) else method_name = matcher.method_name(attr_name) generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1 - if method_defined?(:#{method_name}) - undef :#{method_name} + if method_defined?(:'#{method_name}') + undef :'#{method_name}' end - def #{method_name}(*args) - send(:#{matcher.method_missing_target}, '#{attr_name}', *args) + define_method('#{method_name}') do |*args| + send(:'#{matcher.method_missing_target}', '#{attr_name}', *args) end STR end end end