mjacobus/koine-event-manager

语言: Ruby

git: https://github.com/mjacobus/koine-event-manager

红宝石的活动经理
Event manager for ruby
README.md (中文)

共通语::事件管理器

红宝石的活动经理。

Build Status
Code Coverage
Code Climate
Scrutinizer Code Quality
Dependency Status
Gem Version

安装

将此行添加到应用程序的Gemfile:

gem 'koine-event_manager'

然后执行:

$ bundle

或者自己安装:

$ gem install koine-event_manager

用法

以下是一个简单的例子

event_manager = Koine::EventManager::EventManager.new

event_manager.listen_to(UserSignedIn) do |event|
  WelcomeEmail.new(event.user).send
end

在您的控制器或服务中:

class UserController < ApplicationController
  def create
    @user = User.create(user_params)

    event_manager.trigger(UserSignedIn.new(@user))

    respond_with(@user)
  end
end

您还可以创建事件侦听器:

class UserListener < Koine::EventManager::EventListener
  def initializer
    listen_to(UserSignedIn) do |event|
      WelcomeEmail.new(event.user).send
    end

    listen_to(UserRemovedAccount) do |event|
      PleaseComeBackEmail.new(event.user).send
    end
  end
end

并附加到活动经理:

event_manager = Koine::EventManager::EventManager.new
event_manager.attach_listener(UserListener.new)

# and of course later, you can detach listeners, if you want
event_manager.detach_listener(event_manager.listeners.last)

并在事件管理器上触发事件

event_manager.trigger(UserSignedIn.new(some_user))

向您的类添加事件管理器感知

注意:这使用事件管理器的单例实例。 单身人士通常是一个坏主意,所以更喜欢依赖注入 单例实例。

require "koine/event_manager/event_manager_aware"

class MyModel
  include Koine::EventManager::EventManagerAware

  def some_method
    event_manager.trigger(SomeEvent.new(foo: :bar))
  end
end

以上内容与:

class MyModel
  def some_method
    event_manager.trigger(SomeEvent.new(foo: :bar))
  end

  def event_manager
    Koine::EventManager::EventManager.instance
  end
end

发展

签出repo后,运行bin / setup以安装依赖项。然后,运行rake test来运行测试。您还可以运行bin / console以进行交互式提示,以便进行实验。

要将此gem安装到本地计算机上,请运行bundle exec rake install。要发布新版本,请更新version.rb中的版本号,然后运行bundle exec rake release,它将为版本创建git标记,推送git提交和标记,并将.gem文件推送到rubygems.org。

特约

有关错误报告和请求,欢迎访问GitHub,网址为https://github.com/mjacobus/koine-event-manager。该项目旨在成为一个安全,热情的合作空间,并且贡献者应遵守“贡献者公约”的行为准则。

执照

根据MIT许可条款,gem可作为开源提供。

本文使用googletrans自动翻译,仅供参考, 原文来自github.com

en_README.md

Koine::EventManager

Event manager for ruby.

Build Status
Code Coverage
Code Climate
Scrutinizer Code Quality
Dependency Status
Gem Version

Installation

Add this line to your application's Gemfile:

gem 'koine-event_manager'

And then execute:

$ bundle

Or install it yourself as:

$ gem install koine-event_manager

Usage

Here follows a simple example

event_manager = Koine::EventManager::EventManager.new

event_manager.listen_to(UserSignedIn) do |event|
  WelcomeEmail.new(event.user).send
end

In your controller, or service:

class UserController < ApplicationController
  def create
    @user = User.create(user_params)

    event_manager.trigger(UserSignedIn.new(@user))

    respond_with(@user)
  end
end

You can also create event listeners:

class UserListener < Koine::EventManager::EventListener
  def initializer
    listen_to(UserSignedIn) do |event|
      WelcomeEmail.new(event.user).send
    end

    listen_to(UserRemovedAccount) do |event|
      PleaseComeBackEmail.new(event.user).send
    end
  end
end

And attach to the event manager:

event_manager = Koine::EventManager::EventManager.new
event_manager.attach_listener(UserListener.new)

# and of course later, you can detach listeners, if you want
event_manager.detach_listener(event_manager.listeners.last)

And trigger the event on the event manager

event_manager.trigger(UserSignedIn.new(some_user))

Adding event manager awareness to your classes

CAUTION: This make use of the singleton instance of event manager.
Singletons are usually a bad idea, so prefer dependency injection over
singleton instances.

require "koine/event_manager/event_manager_aware"

class MyModel
  include Koine::EventManager::EventManagerAware

  def some_method
    event_manager.trigger(SomeEvent.new(foo: :bar))
  end
end

The above is the same as:

class MyModel
  def some_method
    event_manager.trigger(SomeEvent.new(foo: :bar))
  end

  def event_manager
    Koine::EventManager::EventManager.instance
  end
end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. 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 tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/mjacobus/koine-event-manager. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.