Class: Cabriolet::Plugin Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/cabriolet/plugin.rb

Overview

This class is abstract.

Subclass and implement #metadata and #setup to create a plugin

Base class for all Cabriolet plugins

Plugins extend Cabriolet’s functionality by providing custom compression algorithms, format handlers, or other enhancements. All plugins must inherit from this base class and implement required methods.

Examples:

Creating a simple plugin

class MyPlugin < Cabriolet::Plugin
  def 
    {
      name: "my-plugin",
      version: "1.0.0",
      author: "Your Name",
      description: "Adds custom compression algorithm",
      cabriolet_version: "~> 0.1"
    }
  end

  def setup
    register_algorithm(:custom, CustomAlgorithm,
                      category: :compressor)
  end
end

Constant Summary collapse

STATES =

Plugin states

i[discovered loaded active failed disabled].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manager = nil) ⇒ Plugin

Initialize a new plugin

Parameters:

  • (defaults to: nil)

    The plugin manager instance



40
41
42
43
# File 'lib/cabriolet/plugin.rb', line 40

def initialize(manager = nil)
  @manager = manager
  @state = :discovered
end

Instance Attribute Details

#stateSymbol (readonly)

Returns Current plugin state.

Returns:

  • Current plugin state



35
36
37
# File 'lib/cabriolet/plugin.rb', line 35

def state
  @state
end

Instance Method Details

#activatevoid

This method returns an undefined value.

Activate the plugin

Called when the plugin is activated. Override to perform actions when the plugin becomes active.

Examples:

Add hooks on activation

def activate
  puts "#{metadata[:name]} activated"
  # Additional activation logic...
end


134
135
136
# File 'lib/cabriolet/plugin.rb', line 134

def activate
  # Default implementation does nothing
end

#cleanupvoid

This method returns an undefined value.

Cleanup the plugin

Called when the plugin is unloaded. Override to perform final cleanup tasks.

Examples:

Final cleanup

def cleanup
  # Release resources...
  # Close connections...
end


166
167
168
# File 'lib/cabriolet/plugin.rb', line 166

def cleanup
  # Default implementation does nothing
end

#deactivatevoid

This method returns an undefined value.

Deactivate the plugin

Called when the plugin is deactivated. Override to perform cleanup when the plugin is deactivated.

Examples:

Cleanup on deactivation

def deactivate
  # Cleanup resources...
  puts "#{metadata[:name]} deactivated"
end


150
151
152
# File 'lib/cabriolet/plugin.rb', line 150

def deactivate
  # Default implementation does nothing
end

#metadataHash

This method is abstract.

Must be implemented by subclasses

Get plugin metadata

Examples:

Minimal metadata

def 
  {
    name: "my-plugin",
    version: "1.0.0",
    author: "Your Name",
    description: "Plugin description",
    cabriolet_version: "~> 0.1"
  }
end

Full metadata

def 
  {
    name: "advanced-plugin",
    version: "2.0.0",
    author: "Developer",
    description: "Advanced features",
    cabriolet_version: ">= 0.1.0",
    homepage: "https://example.com",
    license: "MIT",
    dependencies: ["other-plugin >= 1.0"],
    tags: ["compression", "algorithm"],
    provides: { algorithms: [:custom], formats: [:special] }
  }
end

Parameters:

  • a customizable set of options

Returns:

  • Plugin metadata containing:

Raises:

  • If not implemented by subclass



91
92
93
94
# File 'lib/cabriolet/plugin.rb', line 91

def 
  raise NotImplementedError,
        "#{self.class} must implement metadata method"
end

#setupObject

This method is abstract.

Must be implemented by subclasses

Setup the plugin

Called when the plugin is loaded. Use this method to register algorithms, formats, or perform other initialization tasks.

Examples:

Register an algorithm

def setup
  register_algorithm(:myalgo, MyAlgorithm,
                    category: :compressor)
end

Register multiple items

def setup
  register_algorithm(:algo1, Algo1, category: :compressor)
  register_algorithm(:algo2, Algo2, category: :decompressor)
  register_format(:myformat, MyFormatHandler)
end

Raises:

  • If not implemented by subclass



117
118
119
120
# File 'lib/cabriolet/plugin.rb', line 117

def setup
  raise NotImplementedError,
        "#{self.class} must implement setup method"
end