Class: Rubino::Plugins::Registry

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

Overview

Central registry for plugins and their hooks.

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



7
8
9
10
# File 'lib/rubino/plugins/registry.rb', line 7

def initialize
  @hooks = Hash.new { |h, k| h[k] = [] }
  @plugins = []
end

Instance Method Details

#has_hook?(event) ⇒ Boolean

Returns true if any handlers are registered for this hook

Returns:

  • (Boolean)


30
31
32
# File 'lib/rubino/plugins/registry.rb', line 30

def has_hook?(event)
  @hooks[event.to_sym].any?
end

#load_all!Object

Loads all plugins from configured paths



43
44
45
46
47
48
49
50
51
52
# File 'lib/rubino/plugins/registry.rb', line 43

def load_all!
  plugin_paths.each do |dir|
    expanded = File.expand_path(dir)
    next unless File.directory?(expanded)

    Dir.glob(File.join(expanded, "*.rb")).each do |path|
      load_plugin(path)
    end
  end
end

#load_plugin(path) ⇒ Object

Loads a plugin from a file



35
36
37
38
39
40
# File 'lib/rubino/plugins/registry.rb', line 35

def load_plugin(path)
  load(path)
  @plugins << path
rescue StandardError => e
  Rubino.ui.warning("Failed to load plugin #{path}: #{e.message}")
end

#on(event, &block) ⇒ Object

Registers a hook handler

Raises:



13
14
15
16
17
# File 'lib/rubino/plugins/registry.rb', line 13

def on(event, &block)
  raise Error, "Unknown hook: #{event}. Valid: #{HOOKS.join(", ")}" unless HOOKS.include?(event.to_sym)

  @hooks[event.to_sym] << block
end

#plugin_countObject

Returns count of loaded plugins



55
56
57
# File 'lib/rubino/plugins/registry.rb', line 55

def plugin_count
  @plugins.size
end

#reset!Object

Clears all hooks and plugins (for testing)



60
61
62
63
# File 'lib/rubino/plugins/registry.rb', line 60

def reset!
  @hooks.clear
  @plugins.clear
end

#run_hook(event, context = {}) ⇒ Object

Executes all handlers for a hook, passing context through each



20
21
22
23
24
25
26
27
# File 'lib/rubino/plugins/registry.rb', line 20

def run_hook(event, context = {})
  @hooks[event.to_sym].each do |handler|
    result = handler.call(context)
    # If handler returns a hash, merge it into context
    context = context.merge(result) if result.is_a?(Hash)
  end
  context
end