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.
22 23 24 25 26 |
# File 'lib/kotoshu/plugins/registry.rb', line 22 def initialize @services = {} @plugins = {} @singletons = {} end |
Instance Attribute Details
#plugins ⇒ Hash (readonly)
Returns Registered plugins.
19 20 21 |
# File 'lib/kotoshu/plugins/registry.rb', line 19 def plugins @plugins end |
#services ⇒ Hash (readonly)
Returns Registered services.
16 17 18 |
# File 'lib/kotoshu/plugins/registry.rb', line 16 def services @services end |
Instance Method Details
#clear_singletons ⇒ self
Clear all singletons (force re-instantiation).
96 97 98 99 |
# File 'lib/kotoshu/plugins/registry.rb', line 96 def clear_singletons @singletons.clear self end |
#create_instance(klass) ⇒ Object
Create an instance with dependency injection.
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/kotoshu/plugins/registry.rb', line 71 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.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/kotoshu/plugins/registry.rb', line 51 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.
33 34 35 36 |
# File 'lib/kotoshu/plugins/registry.rb', line 33 def register(name, klass) @services[name] = klass self end |
#register_plugin(plugin) ⇒ self
Register a plugin.
42 43 44 45 |
# File 'lib/kotoshu/plugins/registry.rb', line 42 def register_plugin(plugin) @plugins[plugin.plugin_name] = plugin self end |
#registered?(name) ⇒ Boolean
Check if a service is registered.
89 90 91 |
# File 'lib/kotoshu/plugins/registry.rb', line 89 def registered?(name) @services.key?(name) end |