mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
use class_eval with a string when it's possible
This commit is contained in:
parent
baa237c974
commit
450f7cf01b
3 changed files with 21 additions and 7 deletions
|
@ -106,8 +106,14 @@ module ActiveModel
|
|||
if block_given?
|
||||
sing.send :define_method, name, &block
|
||||
else
|
||||
value = value.to_s if value
|
||||
sing.send(:define_method, name) { value }
|
||||
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
|
||||
|
||||
|
|
|
@ -70,9 +70,13 @@ module ActiveRecord
|
|||
if cache_attribute?(attr_name)
|
||||
access_code = "@attributes_cache['#{attr_name}'] ||= (#{access_code})"
|
||||
end
|
||||
generated_attribute_methods.module_eval do
|
||||
define_method("_#{symbol}") { eval(access_code) }
|
||||
alias_method(symbol, "_#{symbol}")
|
||||
if symbol =~ /^[a-zA-Z_]\w*[!?=]?$/
|
||||
generated_attribute_methods.module_eval("def _#{symbol}; #{access_code}; end; alias #{symbol} _#{symbol}", __FILE__, __LINE__)
|
||||
else
|
||||
generated_attribute_methods.module_eval do
|
||||
define_method("_#{symbol}") { eval(access_code) }
|
||||
alias_method(symbol, "_#{symbol}")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -10,8 +10,12 @@ module ActiveRecord
|
|||
module ClassMethods
|
||||
protected
|
||||
def define_method_attribute=(attr_name)
|
||||
generated_attribute_methods.send(:define_method, "#{attr_name}=") do |new_value|
|
||||
write_attribute(attr_name, new_value)
|
||||
if attr_name =~ /^[a-zA-Z_]\w*[!?=]?$/
|
||||
generated_attribute_methods.module_eval("def #{attr_name}=(new_value); write_attribute('#{attr_name}', new_value); end", __FILE__, __LINE__)
|
||||
else
|
||||
generated_attribute_methods.send(:define_method, "#{attr_name}=") do |new_value|
|
||||
write_attribute(attr_name, new_value)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue