Class: Kotoshu::Plugins::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/kotoshu/plugins/registry.rb

Overview

Registry for managing plugins and their dependencies.

Examples:

Registering a plugin

registry = Registry.new
registry.register(:dictionary, MyDictionary)
registry.register(:suggestions, MySuggestions)

Creating an instance with DI

suggestions = registry.create_instance(MySuggestions)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

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

#pluginsHash (readonly)

Returns Registered plugins.

Returns:

  • (Hash)

    Registered plugins



19
20
21
# File 'lib/kotoshu/plugins/registry.rb', line 19

def plugins
  @plugins
end

#servicesHash (readonly)

Returns Registered services.

Returns:

  • (Hash)

    Registered services



16
17
18
# File 'lib/kotoshu/plugins/registry.rb', line 16

def services
  @services
end

Instance Method Details

#clear_singletonsself

Clear all singletons (force re-instantiation).

Returns:

  • (self)

    Self for chaining



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.

Parameters:

  • klass (Class)

    Class to instantiate

Returns:

  • (Object)

    Created instance



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.message}"
end

#get_service(name) ⇒ Object

Get a service instance.

Parameters:

  • name (Symbol)

    Service name

Returns:

  • (Object)

    Service instance

Raises:



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.

Parameters:

  • name (Symbol)

    Service name

  • klass (Class)

    Service class or instance

Returns:

  • (self)

    Self for chaining



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.

Parameters:

  • plugin (Class)

    Plugin class

Returns:

  • (self)

    Self for chaining



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.

Parameters:

  • name (Symbol)

    Service name

Returns:

  • (Boolean)

    True if registered



89
90
91
# File 'lib/kotoshu/plugins/registry.rb', line 89

def registered?(name)
  @services.key?(name)
end