Module: Castkit::Plugins

Extended by:
Cattri, Cattri::ClassMethods, Cattri::Dsl, Cattri::Visibility
Includes:
Cattri
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)



42
43
44
45
46
47
48
# File 'lib/castkit/plugins.rb', line 42

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



57
58
59
# File 'lib/castkit/plugins.rb', line 57

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:



69
70
71
72
73
74
75
76
77
# File 'lib/castkit/plugins.rb', line 69

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



86
87
88
# File 'lib/castkit/plugins.rb', line 86

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