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.



24
25
26
27
28
# File 'lib/kotoshu/plugins/registry.rb', line 24

def initialize
  @services = {}
  @plugins = {}
  @singletons = {}
end

Instance Attribute Details

#pluginsHash (readonly)

Returns Registered plugins.

Returns:

  • (Hash)

    Registered plugins



21
22
23
# File 'lib/kotoshu/plugins/registry.rb', line 21

def plugins
  @plugins
end

#servicesHash (readonly)

Returns Registered services.

Returns:

  • (Hash)

    Registered services



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

def services
  @services
end

Instance Method Details

#clear_singletonsself

Clear all singletons (force re-instantiation).

Returns:

  • (self)

    Self for chaining



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.

Parameters:

  • klass (Class)

    Class to instantiate

Returns:

  • (Object)

    Created instance



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

#get_service(name) ⇒ Object

Get a service instance.

Parameters:

  • name (Symbol)

    Service name

Returns:

  • (Object)

    Service instance

Raises:



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.

Parameters:

  • name (Symbol)

    Service name

  • klass (Class)

    Service class or instance

Returns:

  • (self)

    Self for chaining



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.

Parameters:

  • plugin (Class)

    Plugin class

Returns:

  • (self)

    Self for chaining



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.

Parameters:

  • name (Symbol)

    Service name

Returns:

  • (Boolean)

    True if registered



91
92
93
# File 'lib/kotoshu/plugins/registry.rb', line 91

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