Sha256: 53afca6cb7f5d6714d958154a7c2f46a2ae1d7da2f9d781b1d72cc63c2a6dc74

Contents?: true

Size: 1.6 KB

Versions: 3

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

require "zeitwerk"

class Rage::CodeLoader
  def initialize
    @reloading = false
  end

  def setup
    @loader = Zeitwerk::Loader.new

    autoload_path = "#{Rage.root}/app"
    enable_reloading = Rage.env.development?
    enable_eager_loading = !Rage.env.development? && !Rage.env.test?

    @loader.push_dir(autoload_path)
    # The first level of directories in app directory won't be treated as modules
    # e.g. app/controllers/pages_controller.rb will be linked to PagesController class
    # instead of Controllers::PagesController
    @loader.collapse("#{Rage.root}/app/*")
    @loader.enable_reloading if enable_reloading
    @loader.setup
    @loader.eager_load if enable_eager_loading
  end

  # in standalone mode - reload the code and the routes
  def reload
    return unless @loader

    @reloading = true
    @loader.reload

    Rage.__router.reset_routes
    load("#{Rage.root}/config/routes.rb")

    unless Rage.autoload?(:Cable) # the `Cable` component is loaded
      Rage::Cable.__router.reset
    end

    unless Rage.autoload?(:OpenAPI) # the `OpenAPI` component is loaded
      Rage::OpenAPI.__reset_data_cache
    end
  end

  # in Rails mode - reset the routes; everything else will be done by Rails
  def rails_mode_reload
    return if @loader

    @reloading = true
    Rage.__router.reset_routes

    unless Rage.autoload?(:Cable) # the `Cable` component is loaded
      Rage::Cable.__router.reset
    end

    unless Rage.autoload?(:OpenAPI) # the `OpenAPI` component is loaded
      Rage::OpenAPI.__reset_data_cache
    end
  end

  def reloading?
    @reloading
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rage-rb-1.13.0 lib/rage/code_loader.rb
rage-rb-1.12.0 lib/rage/code_loader.rb
rage-rb-1.11.0 lib/rage/code_loader.rb