lib/active_model/attribute_methods.rb in activemodel-7.0.0.alpha2 vs lib/active_model/attribute_methods.rb in activemodel-7.0.0.rc1

- old
+ new

@@ -206,11 +206,11 @@ # person.nickname # => "Bob" # person.name_short? # => true # person.nickname_short? # => true def alias_attribute(new_name, old_name) self.attribute_aliases = attribute_aliases.merge(new_name.to_s => old_name.to_s) - CodeGenerator.batch(self, __FILE__, __LINE__) do |code_generator| + ActiveSupport::CodeGenerator.batch(self, __FILE__, __LINE__) do |code_generator| attribute_method_matchers.each do |matcher| method_name = matcher.method_name(new_name).to_s target_name = matcher.method_name(old_name).to_s parameters = matcher.parameters @@ -272,11 +272,11 @@ # def clear_attribute(attr) # send("#{attr}=", nil) # end # end def define_attribute_methods(*attr_names) - CodeGenerator.batch(generated_attribute_methods, __FILE__, __LINE__) do |owner| + ActiveSupport::CodeGenerator.batch(generated_attribute_methods, __FILE__, __LINE__) do |owner| attr_names.flatten.each { |attr_name| define_attribute_method(attr_name, _owner: owner) } end end # Declares an attribute that should be prefixed and suffixed by @@ -307,11 +307,11 @@ # person = Person.new # person.name = 'Bob' # person.name # => "Bob" # person.name_short? # => true def define_attribute_method(attr_name, _owner: generated_attribute_methods) - CodeGenerator.batch(_owner, __FILE__, __LINE__) do |owner| + ActiveSupport::CodeGenerator.batch(_owner, __FILE__, __LINE__) do |owner| attribute_method_matchers.each do |matcher| method_name = matcher.method_name(attr_name) unless instance_method_already_implemented?(method_name) generate_method = "define_method_#{matcher.target}" @@ -356,72 +356,9 @@ end attribute_method_matchers_cache.clear end private - class CodeGenerator # :nodoc: - class MethodSet - METHOD_CACHES = Hash.new { |h, k| h[k] = Module.new } - - def initialize(namespace) - @cache = METHOD_CACHES[namespace] - @sources = [] - @methods = {} - end - - def define_cached_method(name, as: name) - name = name.to_sym - as = as.to_sym - @methods.fetch(name) do - unless @cache.method_defined?(as) - yield @sources - end - @methods[name] = as - end - end - - def apply(owner, path, line) - unless @sources.empty? - @cache.module_eval("# frozen_string_literal: true\n" + @sources.join(";"), path, line) - end - @methods.each do |name, as| - owner.define_method(name, @cache.instance_method(as)) - end - end - end - - class << self - def batch(owner, path, line) - if owner.is_a?(CodeGenerator) - yield owner - else - instance = new(owner, path, line) - result = yield instance - instance.execute - result - end - end - end - - def initialize(owner, path, line) - @owner = owner - @path = path - @line = line - @namespaces = Hash.new { |h, k| h[k] = MethodSet.new(k) } - end - - def define_cached_method(name, namespace:, as: name, &block) - @namespaces[namespace].define_cached_method(name, as: as, &block) - end - - def execute - @namespaces.each_value do |method_set| - method_set.apply(@owner, @path, @line - 1) - end - end - end - private_constant :CodeGenerator - def generated_attribute_methods @generated_attribute_methods ||= Module.new.tap { |mod| include mod } end def instance_method_already_implemented?(method_name)