Class: Kotoshu::Plugins::Registry
- Inherits:
-
Object
- Object
- Kotoshu::Plugins::Registry
- Defined in:
- lib/kotoshu/plugins/registry.rb
Overview
Registry for managing plugins and their dependencies.
Instance Attribute Summary collapse
-
#plugins ⇒ Hash
readonly
Registered plugins.
-
#services ⇒ Hash
readonly
Registered services.
Instance Method Summary collapse
-
#clear_singletons ⇒ self
Clear all singletons (force re-instantiation).
-
#create_instance(klass) ⇒ Object
Create an instance with dependency injection.
-
#get_service(name) ⇒ Object
Get a service instance.
-
#initialize ⇒ Registry
constructor
Create a new registry.
-
#register(name, klass) ⇒ self
Register a service.
-
#register_plugin(plugin) ⇒ self
Register a plugin.
-
#registered?(name) ⇒ Boolean
Check if a service is registered.
Constructor Details
#initialize ⇒ Registry
Create a new registry.
24 25 26 27 28 |
# File 'lib/kotoshu/plugins/registry.rb', line 24 def initialize @services = {} @plugins = {} @singletons = {} end |
Instance Attribute Details
#plugins ⇒ Hash (readonly)
Returns Registered plugins.
21 22 23 |
# File 'lib/kotoshu/plugins/registry.rb', line 21 def plugins @plugins end |
#services ⇒ Hash (readonly)
Returns Registered services.
18 19 20 |
# File 'lib/kotoshu/plugins/registry.rb', line 18 def services @services end |
Instance Method Details
#clear_singletons ⇒ self
Clear all singletons (force re-instantiation).
98 99 100 101 |
# File 'lib/kotoshu/plugins/registry.rb', line 98 def clear_singletons @singletons.clear self end |
#create_instance(klass) ⇒ Object
Create an instance with dependency injection.
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/kotoshu/plugins/registry.rb', line 73 def create_instance(klass) return klass.new unless klass.is_a?(Class) # Get dependencies if it's a Plugin if klass < Plugin dependencies = resolve_dependencies(klass) klass.new(**dependencies) else klass.new end rescue ArgumentError => e raise DependencyError, "Failed to create instance of #{klass}: #{e.}" end |
#get_service(name) ⇒ Object
Get a service instance.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/kotoshu/plugins/registry.rb', line 53 def get_service(name) service = @services[name] raise DependencyError, "Unknown service: #{name}" unless service # Return singleton if already created return @singletons[name] if @singletons.key?(name) # Create new instance instance = service.is_a?(Class) ? service.new : service # Store singleton @singletons[name] = instance if service.is_a?(Class) instance end |
#register(name, klass) ⇒ self
Register a service.
35 36 37 38 |
# File 'lib/kotoshu/plugins/registry.rb', line 35 def register(name, klass) @services[name] = klass self end |
#register_plugin(plugin) ⇒ self
Register a plugin.
44 45 46 47 |
# File 'lib/kotoshu/plugins/registry.rb', line 44 def register_plugin(plugin) @plugins[plugin.plugin_name] = plugin self end |
#registered?(name) ⇒ Boolean
Check if a service is registered.
91 92 93 |
# File 'lib/kotoshu/plugins/registry.rb', line 91 def registered?(name) @services.key?(name) end |