Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Main < Development #46

Merged
merged 8 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.6.1] - 2024-05-17

### Added

- Add `Lennarb::Plugin` module to manage the plugins in the project. Now, the `Lennarb` class is the main class of the project.
- Add `Lennarb::Plugin::Base` class to be the base class of the plugins in the project.
- Add simple guide to use `Lenn` plugins. See [guides/plugins/readme.md](guides/plugins/readme.md) for more details.

## [0.4.4] - 2024-04-02

### Added
Expand Down
69 changes: 69 additions & 0 deletions guides/plugin/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Plugin System

## Overview

The Lennarb Plugin System allows you to extend the functionality of your Lennarb application by registering and loading plugins. This system is designed to be simple and flexible, enabling you to add custom behaviors and features to your application effortlessly.

## Implementation Details

The plugin system is implemented using a module, `Lennarb::Plugin`, which centralizes the registration and loading of plugins.

### Module: `Plugins`

```ruby
class Lennarb
module Plugins
@plugins = {}

def self.register_plugin(name, mod)
@plugins[name] = mod
end

def self.load_plugin(name)
@plugins[name] || raise("Plugin #{name} did not register itself correctly")
end

def self.plugins
@plugins
end
end
end
```

## Usage

### Registering a Plugin

To register a plugin, define a module with the desired functionality and register it with the Plugin module.

```ruby
module MyCustomPlugin
def custom_method
"Custom Method Executed"
end
end

Plugins.register(:my_custom_plugin, MyCustomPlugin)
```

### Load and Use a Plugin

To load and use a plugin in your Lennarb application, call the plugin method in your application class.

```ruby
class MyApp < Lennarb::Application::Base
plugin :my_custom_plugin

get '/custom' do |_req, res|
res.status = 200
res.html(custom_method)
end
end
```

In this example, the custom_method defined in `MyCustomPlugin` is available in the routes of `MyApp`.

## Conclusion

The Lennarb Plugin System provides a simple and flexible way to extend your application's functionality. By registering and loading plugins, you can easily add custom behaviors and features to your Lennarb application.

14 changes: 12 additions & 2 deletions lib/lennarb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
# Released under the MIT License.
# Copyright, 2023-2024, by Aristóteles Coutinho.

ENV['RACK_ENV'] ||= 'development'

# Core extensions
#
require 'pathname'
Expand All @@ -13,6 +11,7 @@
# Base class for Lennarb
#
require_relative 'lennarb/application/base'
require_relative 'lennarb/plugin'
require_relative 'lennarb/request'
require_relative 'lennarb/response'
require_relative 'lennarb/route_node'
Expand Down Expand Up @@ -91,6 +90,17 @@ def patch(path, &block) = add_route(path, :PATCH, block)
def delete(path, &block) = add_route(path, :DELETE, block)
def options(path, &block) = add_route(path, :OPTIONS, block)

# Register a plugin
#
# @parameter [String | Symbol] plugin_name
#
# @returns [void]
#
def plugin(plugin_name)
plugin_module = Lennarb::Plugin.load(plugin_name)
extend plugin_module
end

private

# Add a route
Expand Down
Loading
Loading