Sha256: befde8bef56f26fda330a858bc48f44035bd3d5fdfd8684f17fb8eb9a80f2037

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true
# Ragent::Plugin is reserved for plugins!
module Ragent
  class Plugins
    include Ragent::Logging

    def initialize(ragent)
      @ragent = ragent
      @logger = ragent.logger
      @plugins = {}
      @running_plugins = []
    end

    def load(name, *args, &block)
      info "loading plugin #{name}"
      require "ragent/plugin/#{name}"
      raise "plugin #{name} didn't register" unless @plugins[name.to_s]
      info "loaded plugin #{name}"
      # TODO: load and configure dependencies
      plugin = @plugins[name.to_s]
      info "Configure: #{name}"
      running_plugin = plugin.new(@ragent)
      running_plugin.configure(*args, &block)
      debug "Configured: #{name}"
      @running_plugins << running_plugin
    end

    def register(mod)
      @plugins[mod.plugin_name] = mod
    end

    def start
      @running_plugins.each(&:start)
    end

    def stop
      @running_plugins.each do |plugin|
        info "Stopping: #{plugin.name}"
        plugin.stop
        debug "Stopped: #{plugin.name}"
      end
    end

    private

    def register_commands
      # stop
      cmd = Ragent::Command.new(main: 'plugins',
                                sub: 'list',
                                recipient: self,
                                method: :plugins_list_command)
      @ragent.commands.add(cmd)
    end

    def plugins_list_command(_options)
      @plugins.values.map(&:name).join("\n")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ragent-0.0.5 lib/ragent/plugins.rb