class Sabrina::Plugin
The base class for plugins that improve classes with the ability to handle additional data. The #initialize
method for every plugin should accept a target class instance as the parameter. The #initialize
method of the target class should contain a call to load_plugins
.
When a class has been enhanced by a plugin, it will gain a read-only attribute #plugins
that will contain a set of all plugins registered with that class. Class instances will gain a read-only attribute #plugins
that will contain a hash of SHORT_NAME
=> (plugin instance) pairs.
Each plugin will also expose a set of {FEATURES}. For every feature
, two instance methods will be added to the target object: #feature
, which will call #feature
on every plugin instance that supports it, and #feature_shortname
, which will call #feature
on the instance of the plugin identified by {SHORT_NAME} (assuming it supports that feature).
For clarity, plugins should be contained within the {Plugins} namespace.
Constants
- ENHANCES
An abstract class for dealing with further abstractions of data related to a specific, numbered monster.
See {Sabrina::Plugins} for extensions that might enhance this class with added functionality.
- FEATURES
Tells the enhanced class what public instance methods the plugin should expose.
This should be a set of symbols.
Common features include
:reread
,:write
,:save
,:load
.- PLUGIN_NAME
Plugin
name goes here.- SHORT_NAME
Short name should contain only a-z, 0-9, and underscores. This will be the suffix for target instance methods.
Public Class Methods
Generate a #feature
method.
@param [Symbol] f feature. @return [Proc]
# File lib/sabrina/plugin.rb, line 84 def feature_all(f) proc do |*args| targets = @plugins.select { |_key, val| val.feature?(f) } targets.values.map { |val| val.method(f).call(*args) } end end
Generate a #feature_shortname
method.
@param [Symbol] f feature. @return [Proc]
# File lib/sabrina/plugin.rb, line 95 def feature_this(f, n) proc do |*args| @plugins.fetch(n).method(f).call(*args) end end
@see FEATURES
# File lib/sabrina/plugin.rb, line 67 def features self::FEATURES end
Automagically registers new plugins with target.
# File lib/sabrina/plugin.rb, line 72 def inherited(subclass) target.extend(Plugin::Register) Plugin::Load.include_in(target) target.register_plugin(subclass) super end
Generates a new {Plugin} object.
@param [Object] parent @return [Plugin]
# File lib/sabrina/plugin.rb, line 113 def initialize(parent, *_args) @parent = parent end
@see PLUGIN_NAME
# File lib/sabrina/plugin.rb, line 52 def plugin_name self::PLUGIN_NAME end
@see SHORT_NAME
# File lib/sabrina/plugin.rb, line 62 def short_name self::SHORT_NAME.downcase.gsub(/[^a-z0-9_]/, '') end
@see ENHANCES
# File lib/sabrina/plugin.rb, line 57 def target self::ENHANCES end
Provides a short description of the plugin’s functionality.
@return [String]
# File lib/sabrina/plugin.rb, line 104 def to_s "\'#{short_name}\': enhances #{target.name}, supports #{features.to_a}" end
Public Instance Methods
Whether a plugin instance supports a feature.
@return [Boolean]
# File lib/sabrina/plugin.rb, line 134 def feature?(f) self.class.features.include?(f) end
Drop internal data and force reloading from ROM.
@return [Array] Any return data from child methods.
# File lib/sabrina/plugin.rb, line 120 def reread(*_args) children.map(&:reload_from_rom) end
Subclasses should override this to provide a useful textual representation of instance data. @return [String]
# File lib/sabrina/plugin.rb, line 141 def to_s "<#{self.class.plugin_name}>" end
Write data to ROM.
@return [Array] Any return data from child methods.
# File lib/sabrina/plugin.rb, line 127 def write(*_args) children.map(&:write_to_rom) end