Sha256: bcfe09610406402234f3fbdeef24f5df4358ba9968aee9e70f78dc643c6b6784

Contents?: true

Size: 1.02 KB

Versions: 4

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

require_relative "adapter"
require_relative "handler"

module Lita
  # Constructs a Lita plugin from a block.
  # @since 4.0.0
  # @api private
  class PluginBuilder
    # @param namespace [String, Symbol] The Redis namespace to use for the plugin.
    # @yield The class body of the plugin.
    def initialize(namespace, &block)
      @namespace = namespace.to_s
      @block = block
    end

    # Constructs an {Adapter} from the provided block.
    # @return [Adapter]
    def build_adapter
      adapter = create_plugin(Adapter)
      adapter.class_exec(&@block)
      adapter
    end

    # Constructs a {Handler} from the provided block.
    # @return [Handler]
    def build_handler
      handler = create_plugin(Handler)
      handler.class_exec(&@block)
      handler
    end

    private

    # Creates a class of the relevant plugin type and sets its namespace.
    def create_plugin(plugin_type)
      plugin = Class.new(plugin_type)
      plugin.namespace(@namespace)
      plugin
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rita-5.0.0.alpha.4 lib/lita/plugin_builder.rb
rita-5.0.0.alpha.3 lib/lita/plugin_builder.rb
rita-5.0.0.alpha.2 lib/lita/plugin_builder.rb
rita-5.0.0.alpha.1 lib/lita/plugin_builder.rb