Sha256: 89f2586bc718147efd1b713c87412d2eb45fc09daa1c3bbcc737dbfc456500c5

Contents?: true

Size: 1.56 KB

Versions: 62

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

module ActiveSupport
  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
end

Version data entries

62 entries across 58 versions & 11 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3.4/lib/active_support/code_generator.rb
activesupport-7.0.8.7 lib/active_support/code_generator.rb
activesupport-7.0.8.6 lib/active_support/code_generator.rb
activesupport-7.0.8.5 lib/active_support/code_generator.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.8.4/lib/active_support/code_generator.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3.4/lib/active_support/code_generator.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/code_generator.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/code_generator.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/code_generator.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/code_generator.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/code_generator.rb
activesupport-7.2.0.beta2 lib/active_support/code_generator.rb
activesupport-7.1.3.4 lib/active_support/code_generator.rb
activesupport-7.0.8.4 lib/active_support/code_generator.rb
activesupport-7.2.0.beta1 lib/active_support/code_generator.rb
activesupport-7.1.3.2 lib/active_support/code_generator.rb
activesupport-7.1.3.1 lib/active_support/code_generator.rb
activesupport-7.0.8.1 lib/active_support/code_generator.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3/lib/active_support/code_generator.rb
activesupport-7.1.3 lib/active_support/code_generator.rb