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.
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
require "plugal"
Define a command. It follows the style of JSON#mapping
.
Plugal.command :love, me: String, you: String
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
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.
- Do a lot more testing
- Figure out useful features
- Fork it ( https://github.com/hyronx/plugal/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
- hyronx Fabian Loewe - creator, maintainer