Class: Rage::CodeLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/rage/code_loader.rb

Instance Method Summary collapse

Constructor Details

#initializeCodeLoader

Returns a new instance of CodeLoader.



6
7
8
9
# File 'lib/rage/code_loader.rb', line 6

def initialize
  @reloading = false
  @autoload_path = Rage.root.join("app")
end

Instance Method Details

#check_updated!Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/rage/code_loader.rb', line 66

def check_updated!
  current_watched = @autoload_path.glob("**/*.rb") + Rage.root.glob("config/routes.rb") + Rage.root.glob("config/openapi_components.*")
  current_update_at = current_watched.max_by { |path| path.exist? ? path.mtime.to_f : 0 }&.mtime.to_f
  return false if !@last_watched && !@last_update_at

  current_watched.size != @last_watched.size || current_update_at != @last_update_at

ensure
  @last_watched, @last_update_at = current_watched, current_update_at
end

#rails_mode_reloadObject

in Rails mode - reset the routes; everything else will be done by Rails



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rage/code_loader.rb', line 47

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

#reloadObject

in standalone mode - reload the code and the routes



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rage/code_loader.rb', line 28

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

#reloading?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/rage/code_loader.rb', line 62

def reloading?
  @reloading
end

#setupObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rage/code_loader.rb', line 11

def setup
  @loader = Zeitwerk::Loader.new

  enable_reloading = Rage.env.development?
  enable_eager_loading = !Rage.env.development? && !Rage.env.test?

  @loader.push_dir(@autoload_path.to_s)
  # 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