Skip to content

Commit 01492e3

Browse files
committed
Extract common logic to build mangled method names to a method
1 parent 37833de commit 01492e3

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

activemodel/lib/active_model/attribute_methods.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,8 @@ def alias_attribute_method_definition(code_generator, pattern, new_name, old_nam
225225
method_name = pattern.method_name(new_name).to_s
226226
target_name = pattern.method_name(old_name).to_s
227227
parameters = pattern.parameters
228-
mangled_name = target_name
229228

230-
unless NAME_COMPILABLE_REGEXP.match?(target_name)
231-
mangled_name = "__temp__#{target_name.unpack1("h*")}"
232-
end
229+
mangled_name = build_mangled_name(target_name)
233230

234231
code_generator.define_cached_method(method_name, as: mangled_name, namespace: :alias_attribute) do |batch|
235232
body = if CALL_COMPILABLE_REGEXP.match?(target_name)
@@ -419,10 +416,7 @@ def attribute_method_patterns_matching(method_name)
419416
# using the given `extra` args. This falls back on `send`
420417
# if the called name cannot be compiled.
421418
def define_proxy_call(code_generator, name, proxy_target, parameters, *call_args, namespace:)
422-
mangled_name = name
423-
unless NAME_COMPILABLE_REGEXP.match?(name)
424-
mangled_name = "__temp__#{name.unpack1("h*")}"
425-
end
419+
mangled_name = build_mangled_name(name)
426420

427421
call_args.map!(&:inspect)
428422
call_args << parameters if parameters
@@ -445,6 +439,16 @@ def define_proxy_call(code_generator, name, proxy_target, parameters, *call_args
445439
end
446440
end
447441

442+
def build_mangled_name(name)
443+
mangled_name = name
444+
445+
unless NAME_COMPILABLE_REGEXP.match?(name)
446+
mangled_name = "__temp__#{name.unpack1("h*")}"
447+
end
448+
449+
mangled_name
450+
end
451+
448452
class AttributeMethodPattern # :nodoc:
449453
attr_reader :prefix, :suffix, :proxy_target, :parameters
450454

0 commit comments

Comments
 (0)