Module: Castkit::Plugins

Defined in:
lib/castkit/castkit.rb,
lib/castkit/plugins.rb

Overview

Internal registry for Castkit plugin modules.

This module supports registering, activating, and looking up Castkit plugins. Plugins are typically included in Castkit::DataObject subclasses via ‘.enable_plugins`.

Plugins can be defined under ‘Castkit::Plugins::<Name>` or manually registered through the configuration API.

Examples:

Registering a custom plugin

Castkit.configure do |config|
  config.register_plugin(:custom, MyCustomPlugin)
end

Enabling plugins on a DataObject

class MyDto < Castkit::DataObject
  enable_plugins :custom, :oj
end

Class Method Summary collapse

Class Method Details

.activate(klass, *names) ⇒ void

This method returns an undefined value.

Activates one or more plugins on the given class.

Each plugin module is included into the class. If the module responds to ‘setup!`, it will be called with the class as the argument.

Parameters:

  • klass (Class)

    the target class (usually a Castkit::DataObject subclass)

  • names (Array<Symbol>)

    plugin names (e.g., :oj, :yaml)



33
34
35
36
37
38
39
# File 'lib/castkit/plugins.rb', line 33

def activate(klass, *names)
  names.each do |name|
    plugin = lookup!(name)
    klass.include(plugin) if plugin
    plugin.setup!(klass) if plugin.respond_to?(:setup!)
  end
end

.deactivate(_klass, *names) ⇒ void

This method returns an undefined value.

(Placeholder) Deactivates plugins by name.

Currently not implemented, included for future API completeness.

Parameters:

  • _klass (Class)

    the class to deactivate plugins on

  • names (Array<Symbol>)

    plugin names to deactivate



48
49
50
# File 'lib/castkit/plugins.rb', line 48

def deactivate(_klass, *names)
  @deactivate_plugins = names
end

.lookup!(name) ⇒ Module

Looks up a plugin module by name.

This will first check the internal registry, then fall back to resolving a constant under ‘Castkit::Plugins::<Name>`.

Parameters:

  • name (Symbol, String)

    the plugin name (e.g., :oj)

Returns:

  • (Module)

    the plugin module

Raises:



60
61
62
63
64
65
66
67
68
# File 'lib/castkit/plugins.rb', line 60

def lookup!(name)
  @registered_plugins[name.to_sym] ||
    const_get(Castkit::Inflector.pascalize(name.to_s), false)
rescue NameError
  raise Castkit::Error,
        "Castkit plugin `#{name}` could not be found. Make sure it is " \
        "defined under Castkit::Plugins or registered using " \
        "`Castkit.configure { |c| c.register_plugin(:#{name}, MyPlugin) }`."
end

.register(name, plugin) ⇒ void

This method returns an undefined value.

Registers a plugin module under a custom name.

This allows developers to register modules not defined under Castkit::Plugins.

Parameters:

  • name (Symbol)

    the plugin name (e.g., :custom_plugin)

  • plugin (Module)

    the plugin module to register



77
78
79
# File 'lib/castkit/plugins.rb', line 77

def register(name, plugin)
  @registered_plugins[name] = plugin
end