mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Also batch attribute readers and writers
This commit is contained in:
parent
348e142b26
commit
d12418a776
4 changed files with 30 additions and 31 deletions
|
@ -294,7 +294,7 @@ module ActiveModel
|
||||||
generate_method = "define_method_#{matcher.target}"
|
generate_method = "define_method_#{matcher.target}"
|
||||||
|
|
||||||
if respond_to?(generate_method, true)
|
if respond_to?(generate_method, true)
|
||||||
send(generate_method, attr_name.to_s)
|
send(generate_method, attr_name.to_s, owner: owner)
|
||||||
else
|
else
|
||||||
define_proxy_call true, owner, method_name, matcher.target, attr_name.to_s
|
define_proxy_call true, owner, method_name, matcher.target, attr_name.to_s
|
||||||
end
|
end
|
||||||
|
@ -354,14 +354,23 @@ module ActiveModel
|
||||||
@path = path
|
@path = path
|
||||||
@line = line
|
@line = line
|
||||||
@sources = ["# frozen_string_literal: true\n"]
|
@sources = ["# frozen_string_literal: true\n"]
|
||||||
|
@renames = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
def <<(source_line)
|
def <<(source_line)
|
||||||
@sources << source_line
|
@sources << source_line
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def rename_method(old_name, new_name)
|
||||||
|
@renames[old_name] = new_name
|
||||||
|
end
|
||||||
|
|
||||||
def execute
|
def execute
|
||||||
@owner.module_eval(@sources.join(";"), @path, @line - 1)
|
@owner.module_eval(@sources.join(";"), @path, @line - 1)
|
||||||
|
@renames.each do |old_name, new_name|
|
||||||
|
@owner.alias_method new_name, old_name
|
||||||
|
@owner.undef_method old_name
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
private_constant :CodeGenerator
|
private_constant :CodeGenerator
|
||||||
|
@ -527,7 +536,7 @@ module ActiveModel
|
||||||
# to allocate an object on each call to the attribute method.
|
# to allocate an object on each call to the attribute method.
|
||||||
# Making it frozen means that it doesn't get duped when used to
|
# Making it frozen means that it doesn't get duped when used to
|
||||||
# key the @attributes in read_attribute.
|
# key the @attributes in read_attribute.
|
||||||
def self.define_attribute_accessor_method(mod, attr_name, writer: false)
|
def self.define_attribute_accessor_method(owner, attr_name, writer: false)
|
||||||
method_name = "#{attr_name}#{'=' if writer}"
|
method_name = "#{attr_name}#{'=' if writer}"
|
||||||
if attr_name.ascii_only? && DEF_SAFE_NAME.match?(attr_name)
|
if attr_name.ascii_only? && DEF_SAFE_NAME.match?(attr_name)
|
||||||
yield method_name, "'#{attr_name}'"
|
yield method_name, "'#{attr_name}'"
|
||||||
|
@ -538,8 +547,7 @@ module ActiveModel
|
||||||
temp_method_name = "__temp__#{safe_name}#{'=' if writer}"
|
temp_method_name = "__temp__#{safe_name}#{'=' if writer}"
|
||||||
attr_name_expr = "::ActiveModel::AttributeMethods::AttrNames::#{const_name}"
|
attr_name_expr = "::ActiveModel::AttributeMethods::AttrNames::#{const_name}"
|
||||||
yield temp_method_name, attr_name_expr
|
yield temp_method_name, attr_name_expr
|
||||||
mod.alias_method method_name, temp_method_name
|
owner.rename_method(temp_method_name, method_name)
|
||||||
mod.undef_method temp_method_name
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -42,17 +42,14 @@ module ActiveModel
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def define_method_attribute=(name)
|
def define_method_attribute=(name, owner:)
|
||||||
ActiveModel::AttributeMethods::AttrNames.define_attribute_accessor_method(
|
ActiveModel::AttributeMethods::AttrNames.define_attribute_accessor_method(
|
||||||
generated_attribute_methods, name, writer: true,
|
owner, name, writer: true,
|
||||||
) do |temp_method_name, attr_name_expr|
|
) do |temp_method_name, attr_name_expr|
|
||||||
generated_attribute_methods.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
owner <<
|
||||||
# frozen_string_literal: true
|
"def #{temp_method_name}(value)" <<
|
||||||
def #{temp_method_name}(value)
|
" write_attribute(#{attr_name_expr}, value)" <<
|
||||||
name = #{attr_name_expr}
|
"end"
|
||||||
write_attribute(name, value)
|
|
||||||
end
|
|
||||||
RUBY
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -7,17 +7,14 @@ module ActiveRecord
|
||||||
|
|
||||||
module ClassMethods # :nodoc:
|
module ClassMethods # :nodoc:
|
||||||
private
|
private
|
||||||
def define_method_attribute(name)
|
def define_method_attribute(name, owner:)
|
||||||
ActiveModel::AttributeMethods::AttrNames.define_attribute_accessor_method(
|
ActiveModel::AttributeMethods::AttrNames.define_attribute_accessor_method(
|
||||||
generated_attribute_methods, name
|
owner, name
|
||||||
) do |temp_method_name, attr_name_expr|
|
) do |temp_method_name, attr_name_expr|
|
||||||
generated_attribute_methods.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
owner <<
|
||||||
# frozen_string_literal: true
|
"def #{temp_method_name}" <<
|
||||||
def #{temp_method_name}
|
" _read_attribute(#{attr_name_expr}) { |n| missing_attribute(n, caller) }" <<
|
||||||
name = #{attr_name_expr}
|
"end"
|
||||||
_read_attribute(name) { |n| missing_attribute(n, caller) }
|
|
||||||
end
|
|
||||||
RUBY
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -11,17 +11,14 @@ module ActiveRecord
|
||||||
|
|
||||||
module ClassMethods # :nodoc:
|
module ClassMethods # :nodoc:
|
||||||
private
|
private
|
||||||
def define_method_attribute=(name)
|
def define_method_attribute=(name, owner:)
|
||||||
ActiveModel::AttributeMethods::AttrNames.define_attribute_accessor_method(
|
ActiveModel::AttributeMethods::AttrNames.define_attribute_accessor_method(
|
||||||
generated_attribute_methods, name, writer: true,
|
owner, name, writer: true,
|
||||||
) do |temp_method_name, attr_name_expr|
|
) do |temp_method_name, attr_name_expr|
|
||||||
generated_attribute_methods.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
owner <<
|
||||||
# frozen_string_literal: true
|
"def #{temp_method_name}(value)" <<
|
||||||
def #{temp_method_name}(value)
|
" _write_attribute(#{attr_name_expr}, value)" <<
|
||||||
name = #{attr_name_expr}
|
"end"
|
||||||
_write_attribute(name, value)
|
|
||||||
end
|
|
||||||
RUBY
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue