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.
Class Method Summary collapse
-
.activate(klass, *names) ⇒ void
Activates one or more plugins on the given class.
-
.deactivate(_klass, *names) ⇒ void
(Placeholder) Deactivates plugins by name.
-
.lookup!(name) ⇒ Module
Looks up a plugin module by name.
-
.register(name, plugin) ⇒ void
Registers a plugin module under a custom name.
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.
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.
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>`.
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.
86 87 88 |
# File 'lib/castkit/plugins.rb', line 86 def register(name, plugin) registered_plugins[name.to_sym] = plugin end |