Eztek MediatR

Buy Me A Coffee

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file lib/eztek/mediat_r. To experiment with that code, run bin/console for an interactive prompt.

Installation

Add this line to your application's Gemfile:

gem 'eztek-mediat_r'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install eztek-mediat_r

Usage

Initialize mediatR

Add new file config/initializers/mediat_r.rb to initializer mediatR

require 'ez/mediat_r'

mediator = EZ::MediatR.new
Rails.configuration.to_prepare do
    Rails.configuration.bus = Bus.new
    Rails.configuration.bus.tap do |bus|
        # Register command - handler
        bus.register(Auth::Commands::LoginCommand, Auth::Commands::Handlers::LoginCommandHandler.new)
    end
end

Declare mediatR in controller

def initialize
    @mediator = Rails.configuration.bus
end

or dependency with dry-container, dry-auto_inject:

Register mediaR in container

Use file dependency.rb to register mediatR

require 'ez/mediat_r'

dependency_container = Dry::Container.new
dependency_container.register('mediator', -> { EZ::MediatR.new })
AutoInject = Dry::AutoInject(dependency_container)

Define mediatR in controller

include AutoInject['mediator']

Run mediator

command = Auth::Commands::LoginCommand.new(username, password)
mediator.execute(command) 

Generator structure with rails

    $ rails generate command feature command_name

Example:

    $ rails generate command auth login

This will create: app/cqrs/auth/commands/login_command.rb app/cqrs/auth/commands/validators/login_command_validator.rb app/cqrs/auth/commands/handlers/login_command_handler.rb

Development

After checking out the repo, run bin/setup to install dependencies. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/yeucon02vn/eztek-mediat_r.