Extremely lightweight message bus without mixins, globals or mutable state.
Add this line to your application's Gemfile:
gem 'bus'
And then execute:
$ bundle
Or install it yourself as:
$ gem install bus
# global config or initializer or application controller
bus = Bus.new([
DonationReceipt,
RealtimeViewerUpdate
])
# controller
class DonationsController # < ActionController::Base
def create
listener = bus.
on(:donation_created) {|donation| send_user_to_payment_gateway(donation) }. # NOTE the dot!
on(:donation_invalid) {|donation| render donation.errors, status: :bad_request }
# or
listener = bus.on(
donation_created: ->(donation) { send_user_to_payment_gateway(donation) },
donation_invalid: ->(donation) { render donation.errors, status: :bad_request }
)
DonationService.new.add_donation(current_user, params[:donation], listener)
# or
DonationService.new.add_donation(current_user, params[:donation], bus.when(
donation_created: ->(donation) { send_user_to_payment_gateway(donation) },
donation_invalid: ->(donation) { render donation.errors, status: :bad_request }
))
end
private
def send_user_to_payment_gateway(donation)
#...
end
end
# service
class DonationService
def add_donation(donor, donation_attributes, listener)
donation = donor.donations.build(donation_attributes)
if donation.save
listener.donation_created(donation)
else
listener.donation_invalid(donation)
end
end
end
# listeners
class DonationReceipt
def donation_created(donation)
Mailer.delay.send_donation_receipt(donation)
end
end
class RealtimeViewerUpdate
def donation_created(donation)
#..
end
end
- Fork it ( http://github.com/minio-sk/bus/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 new Pull Request