Dispatch ActiveRecord objects to any structures with ease.
Installation
Add this line to your application's Gemfile:
gem 'redispatcher'
And then execute:
$ bundle
Or install it yourself as:
$ gem install redispatcher
Using
Writing Dispatchers
Dispatchers inherit from Redispatcher::Dispatcher
, live in your app/dispatchers
directory, and are named for the model that they dispatch:
# app/dispatchers/topic_dispatcher.rb
class TopicDispatcher < Redispatcher::Dispatcher
end
Do not hesitate to use dispatscher's callbacks before_
and after_
initialize
, process
, commit
, rollback
just like that:
# app/dispatchers/topic_dispatcher.rb
class TopicDispatcher < Redispatcher::Dispatcher
after_initialize do
@processed_object = {}
end
before_process do
@processed_object.merge! title: object.title
end
after_commit :update_mongodb
def update_mongodb
MONGO['topics'].update({ id: object.id }, processed_object, upsert: true)
end
end
Enable dispatcher for your model
class Topic < ActiveRecord::Base
dispatchable
end
Dispatch!
Just call dispatch method on object you going to dispatch.
dispatched_topic = Topic.first.dispatch
Contributing
- Fork it ( https://github.com/rambler-digital-solutions/redispatcher/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