Skip to content

Commit 139e806

Browse files
committed
Remove duplication between alias_attribute_method_definition and define_proxy_call
1 parent 8ec3843 commit 139e806

File tree

1 file changed

+18
-28
lines changed

1 file changed

+18
-28
lines changed

activemodel/lib/active_model/attribute_methods.rb

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -231,21 +231,7 @@ def alias_attribute_method_definition(code_generator, pattern, new_name, old_nam
231231
call_args = []
232232
call_args << parameters if parameters
233233

234-
code_generator.define_cached_method(method_name, as: mangled_name, namespace: :alias_attribute) do |batch|
235-
body = if CALL_COMPILABLE_REGEXP.match?(target_name)
236-
"self.#{target_name}(#{call_args.join(", ")})"
237-
else
238-
call_args.unshift(":'#{target_name}'")
239-
"send(#{call_args.join(", ")})"
240-
end
241-
242-
modifier = parameters == FORWARD_PARAMETERS ? "ruby2_keywords " : ""
243-
244-
batch <<
245-
"#{modifier}def #{mangled_name}(#{parameters || ''})" <<
246-
body <<
247-
"end"
248-
end
234+
define_call(code_generator, method_name, target_name, mangled_name, parameters, call_args, namespace: :alias_attribute)
249235
end
250236

251237
# Is +new_name+ an alias?
@@ -424,11 +410,25 @@ def define_proxy_call(code_generator, name, proxy_target, parameters, *call_args
424410
call_args << parameters if parameters
425411
namespace = :"#{namespace}_#{proxy_target}_#{call_args.join("_")}}"
426412

413+
define_call(code_generator, name, proxy_target, mangled_name, parameters, call_args, namespace: namespace)
414+
end
415+
416+
def build_mangled_name(name)
417+
mangled_name = name
418+
419+
unless NAME_COMPILABLE_REGEXP.match?(name)
420+
mangled_name = "__temp__#{name.unpack1("h*")}"
421+
end
422+
423+
mangled_name
424+
end
425+
426+
def define_call(code_generator, name, target_name, mangled_name, parameters, call_args, namespace:)
427427
code_generator.define_cached_method(name, as: mangled_name, namespace: namespace) do |batch|
428-
body = if CALL_COMPILABLE_REGEXP.match?(proxy_target)
429-
"self.#{proxy_target}(#{call_args.join(", ")})"
428+
body = if CALL_COMPILABLE_REGEXP.match?(target_name)
429+
"self.#{target_name}(#{call_args.join(", ")})"
430430
else
431-
call_args.unshift(":'#{proxy_target}'")
431+
call_args.unshift(":'#{target_name}'")
432432
"send(#{call_args.join(", ")})"
433433
end
434434

@@ -441,16 +441,6 @@ def define_proxy_call(code_generator, name, proxy_target, parameters, *call_args
441441
end
442442
end
443443

444-
def build_mangled_name(name)
445-
mangled_name = name
446-
447-
unless NAME_COMPILABLE_REGEXP.match?(name)
448-
mangled_name = "__temp__#{name.unpack1("h*")}"
449-
end
450-
451-
mangled_name
452-
end
453-
454444
class AttributeMethodPattern # :nodoc:
455445
attr_reader :prefix, :suffix, :proxy_target, :parameters
456446

0 commit comments

Comments
 (0)