Class: Gruf::Controllers::Autoloader

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/gruf/controllers/autoloader.rb

Overview

Handles autoloading of Gruf controllers in the application path. This allows for code reloading on Gruf controllers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#logger

Constructor Details

#initialize(path:, reloading: nil, tag: nil) ⇒ Autoloader

Returns a new instance of Autoloader.

Parameters:

  • path (String)
  • reloading (Boolean) (defaults to: nil)
  • tag (String) (defaults to: nil)


38
39
40
41
42
43
44
45
46
# File 'lib/gruf/controllers/autoloader.rb', line 38

def initialize(path:, reloading: nil, tag: nil)
  super()
  @path = path
  @loader = ::Zeitwerk::Loader.new
  @loader.tag = tag || 'gruf-controllers'
  @setup = false
  @reloading_enabled = reloading || ::Gruf.development?
  setup!
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



31
32
33
# File 'lib/gruf/controllers/autoloader.rb', line 31

def path
  @path
end

Instance Method Details

#reloadObject

Reload all files managed by the autoloader, if reloading is enabled



51
52
53
54
55
56
57
# File 'lib/gruf/controllers/autoloader.rb', line 51

def reload
  return unless @reloading_enabled

  reload_lock.with_write_lock do
    @loader.reload
  end
end

#with_fresh_controller(controller_name) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/gruf/controllers/autoloader.rb', line 59

def with_fresh_controller(controller_name)
  return yield(controller_name.constantize) unless @reloading_enabled

  ::Gruf::Autoloaders.reload
  reload_lock.with_read_lock do
    yield(controller_name.constantize)
  end
end