Class: SleepingKingStudios::Tools::Toolbox::Initializer
- Inherits:
-
Object
- Object
- SleepingKingStudios::Tools::Toolbox::Initializer
- Defined in:
- lib/sleeping_king_studios/tools/toolbox/initializer.rb
Overview
Utility class for initializing a library or project.
An initializer is used to set up the project, including loading configuration, setting initial data or states, and initializing dependencies.
Using an initializer provides several advantages:
-
Potentially-expensive setup code is run on your own schedule, not when the module is first loaded.
-
The setup code will only run once, even if the initializer is called multiple times or from multiple places.
-
The setup code will only run once, even in a multi-threaded environment.
Instance Method Summary collapse
-
#call ⇒ Object
Runs the initialization block exactly once.
-
#initialize { ... } ⇒ Initializer
constructor
A new instance of Initializer.
Constructor Details
#initialize { ... } ⇒ Initializer
Returns a new instance of Initializer.
42 43 44 45 46 47 48 |
# File 'lib/sleeping_king_studios/tools/toolbox/initializer.rb', line 42 def initialize(&block) raise ArgumentError, 'no block given' unless block_given? @block = block @called = false @semaphore = Thread::Mutex.new end |
Instance Method Details
#call ⇒ Object
Runs the initialization block exactly once.
51 52 53 54 55 56 57 58 59 |
# File 'lib/sleeping_king_studios/tools/toolbox/initializer.rb', line 51 def call @semaphore.synchronize do return if @called @block.call @called = true end end |