Class: Gruf::Controllers::Autoloader
- Inherits:
-
Object
- Object
- Gruf::Controllers::Autoloader
- 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
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(path:, reloading: nil, tag: nil) ⇒ Autoloader
constructor
A new instance of Autoloader.
-
#reload ⇒ Object
Reload all files managed by the autoloader, if reloading is enabled.
- #with_fresh_controller(controller_name) ⇒ Object
Methods included from Loggable
Constructor Details
#initialize(path:, reloading: nil, tag: nil) ⇒ Autoloader
Returns a new instance of Autoloader.
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
#path ⇒ Object (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
#reload ⇒ Object
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 |