This gem provides delivery of objects from database to any structure with power of callbacks.
Supporting ORM are ActiveRecord and Sequel (through sequel-rails gem)
Add this line to your application's Gemfile:
gem 'redispatcher'
And then execute:
$ bundle
Or install it yourself as:
$ gem install redispatcher
Dispatchers inherit from Redispatcher::Dispatcher
, live in your app/dispatchers
directory, and are named for the model that they dispatch.
You can use rails generator to generate dispatcher:
rails g dispatcher Topic
This will create TopicDispatcher:
# app/dispatchers/topic_dispatcher.rb
class TopicDispatcher < Redispatcher::Dispatcher
# ...
end
Do not hesitate to use dispatscher's callbacks before_initialize
, after_initialize
, before_process
, after_process
, before_commit
, after_commit
, before_rollback
, after_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
With ActiveRecord:
class Topic < ActiveRecord::Base
dispatchable
end
With Sequel:
class Topic < Sequel::Model
dispatchable
end
Just call dispatch method on object you going to dispatch.
dispatched_topic = Topic.first.dispatch
Redispatcher is licensed under the MIT license. See LICENSE for full license text.
- 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