Skip to content

RunsFor/redispatcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Redispatcher

Gem Version Code Climate Test Coverage Build Status

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)

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.

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

Enable dispatcher for your model

With ActiveRecord:

class Topic < ActiveRecord::Base
  dispatchable
end

With Sequel:

class Topic < Sequel::Model
  dispatchable
end

Dispatch!

Just call dispatch method on object you going to dispatch.

dispatched_topic = Topic.first.dispatch

License

Redispatcher is licensed under the MIT license. See LICENSE for full license text.

Contributing

  1. Fork it ( https://github.com/rambler-digital-solutions/redispatcher/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