/plugal

A plugin system relying on Redis written in Crystal

Primary LanguageCrystalMIT LicenseMIT

Plugal

A plugin system relying on Redis in Crystal

Beside that you can also use it to simply communicate with other processes or applications over commands in a type-safer way.

Installation

Install Redis. For information on how to do that have a look at http://redis.io.

Then add this to your application's shard.yml:

dependencies:
  plugal:
    github: hyronx/plugal

Usage

require "plugal"

Define a command. It follows the style of JSON#mapping.

Plugal.command :love, me: String, you: String

The Provider

Create a provider which includes Plugal::Provider.

class MyProvider
  include Plugal::Provider

  def initialize
    @@receiver = "MyReceiver"
  end

  provide :love do |me, you|
    "#{me}+#{you}=LoVe!"
  end
end

Now you can create an instance of your class and run it. This will already be the first application and could serve as a plugin.

prov = MyProvider.new
prov.run

The Receiver

To receive the data provided by the first class you need a receiver. Quelle surprise! Include Plugal::Receiver and use the Plugal::Receiver#receive macro to get your result.

class MyReceiver
  include Plugal::Receiver

  receive :love do |result|
    puts result.data  # => e.g. "Victor+Victoria=LoVe!"
  end
end

There are some things to notice. You don't directly get your resulting data but a wrapper containing it. The Plugal::Result class can also signal you failure so you can handle errors. For more information check the docs.

Again an instance must be created and run. This application will receive data from all providers connected to it. The block gets called after the Redis subscriptions are made and can be used to actually send commands.

recv = MyReceiver.new
recv.run do
  recv.send :love, me: "Victor", you: "Victoria"
end

To see this example at work have a look here: plugal-test.

Development

  1. Do a lot more testing
  2. Figure out useful features

Contributing

  1. Fork it ( https://github.com/hyronx/plugal/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • hyronx Fabian Loewe - creator, maintainer