Sha256: b575865a84b3e617059c1707d057bb7f49ceb760d574fb5275a4df2591496054

Contents?: true

Size: 1.95 KB

Versions: 5

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

# Try to require zeitwerk
begin
  require "zeitwerk"
  require "active_support/dependencies/zeitwerk_integration"
rescue LoadError
end

module Anyway
  class Settings
    class << self
      attr_reader :autoload_static_config_path, :autoloader

      if defined?(::Zeitwerk)
        def autoload_static_config_path=(val)
          raise "Cannot setup autoloader after application has been initialized" if ::Rails.application.initialized?

          return unless ::Rails.root.join(val).exist?

          autoloader&.unload

          @autoload_static_config_path = val

          # See https://github.com/rails/rails/blob/8ab4fd12f18203b83d0f252db96d10731485ff6a/railties/lib/rails/autoloaders.rb#L10
          @autoloader = Zeitwerk::Loader.new.tap do |loader|
            loader.tag = "anyway.config"
            loader.inflector = ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector
            loader.push_dir(::Rails.root.join(val))
            loader.setup
          end
        end

        def cleanup_autoload_paths
          return unless autoload_static_config_path
          ActiveSupport::Dependencies.autoload_paths.delete(::Rails.root.join(autoload_static_config_path).to_s)
        end
      else
        def autoload_static_config_path=(val)
          if autoload_static_config_path
            old_path = ::Rails.root.join(autoload_static_config_path).to_s
            ActiveSupport::Dependencies.autoload_paths.delete(old_path)
            ::Rails.application.config.eager_load_paths.delete(old_path)
          end

          @autoload_static_config_path = val
          new_path = ::Rails.root.join(val).to_s
          ActiveSupport::Dependencies.autoload_paths << new_path
          ::Rails.application.config.eager_load_paths << new_path
        end

        def cleanup_autoload_paths
          :no_op
        end
      end
    end

    self.default_config_path = ->(name) { ::Rails.root.join("config", "#{name}.yml") }
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
anyway_config-2.0.6 lib/anyway/rails/settings.rb
anyway_config-2.0.5 lib/anyway/rails/settings.rb
anyway_config-2.0.4 lib/anyway/rails/settings.rb
anyway_config-2.0.3 lib/anyway/rails/settings.rb
anyway_config-2.0.2 lib/anyway/rails/settings.rb