Module: Castkit::Ext::DataObject::Plugins
- Included in:
- DataObject
- Defined in:
- lib/castkit/ext/data_object/plugins.rb
Overview
Provides plugin support for DataObject classes.
This module allows a Castkit::DataObject to explicitly declare supported plugins, and ensures all default plugins are enabled on subclassing.
Plugins should be defined under ‘Castkit::Plugins::<Name>` and can be registered globally via `Castkit.configure { |c| c.register_plugin(:name, MyPlugin) }`.
Example:
class MyDto < Castkit::DataObject
enable_plugins :oj, :msgpack
disable_plugins :yaml
end
Instance Method Summary collapse
-
#disable_plugins(*plugins) ⇒ void
Disables one or more default plugins on the calling class.
-
#disabled_plugins ⇒ Set<Symbol>
Returns the set of default plugins explicitly disabled on the class.
-
#enable_plugins(*plugins) ⇒ void
Enables one or more plugins on the calling class.
-
#enabled_plugins ⇒ Set<Symbol>
Returns the set of plugins explicitly enabled on the class.
-
#inherited(subclass) ⇒ void
Hook that is called when a DataObject subclass is created.
Instance Method Details
#disable_plugins(*plugins) ⇒ void
This method returns an undefined value.
Disables one or more default plugins on the calling class.
61 62 63 64 65 66 |
# File 'lib/castkit/ext/data_object/plugins.rb', line 61 def disable_plugins(*plugins) return if plugins.empty? @disabled_plugins ||= Set.new @disabled_plugins.merge(plugins) end |
#disabled_plugins ⇒ Set<Symbol>
Returns the set of default plugins explicitly disabled on the class.
31 32 33 |
# File 'lib/castkit/ext/data_object/plugins.rb', line 31 def disabled_plugins @disabled_plugins ||= Set.new end |
#enable_plugins(*plugins) ⇒ void
This method returns an undefined value.
Enables one or more plugins on the calling class.
39 40 41 42 43 44 45 46 |
# File 'lib/castkit/ext/data_object/plugins.rb', line 39 def enable_plugins(*plugins) return if plugins.empty? @enabled_plugins ||= Set.new @enabled_plugins.merge(plugins) Castkit::Plugins.activate(self, *plugins) end |
#enabled_plugins ⇒ Set<Symbol>
Returns the set of plugins explicitly enabled on the class.
24 25 26 |
# File 'lib/castkit/ext/data_object/plugins.rb', line 24 def enabled_plugins @enabled_plugins ||= Set.new end |
#inherited(subclass) ⇒ void
This method returns an undefined value.
Hook that is called when a DataObject subclass is created.
Automatically applies ‘Castkit.configuration.default_plugins` to the subclass.
75 76 77 78 79 80 81 82 |
# File 'lib/castkit/ext/data_object/plugins.rb', line 75 def inherited(subclass) super disabled = instance_variable_get(:@disabled_plugins) || Set.new plugins = Castkit.configuration.default_plugins - disabled.to_a subclass.enable_plugins(*plugins) end |