https://github.com/dcsg/eventception
Eventception is a lightweight and simple Ruby Event System
https://github.com/dcsg/eventception
event-dispatcher event-driven events jruby ruby-gem
Last synced: 1 day ago
JSON representation
Eventception is a lightweight and simple Ruby Event System
- Host: GitHub
- URL: https://github.com/dcsg/eventception
- Owner: dcsg
- License: lgpl-3.0
- Created: 2017-07-09T21:59:38.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-07-22T21:33:39.000Z (almost 8 years ago)
- Last Synced: 2025-04-01T15:13:49.243Z (22 days ago)
- Topics: event-dispatcher, event-driven, events, jruby, ruby-gem
- Language: Ruby
- Homepage: https://dcsg.github.io/eventception/
- Size: 128 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
## Eventception
[](https://travis-ci.org/dcsg/eventception) [](https://codeclimate.com/github/dcsg/eventception) [](https://codeclimate.com/github/dcsg/eventception/coverage)
Eventception is a lightweight and simple Ruby Event System inspired on [Symfony Event Dispatcher](https://symfony.com/doc/current/components/event_dispatcher.html).
## How to Install
Add the following to your `Gemfile`:
```ruby
gem 'eventception', '~> 0.1.0'
```## How to use
You can also read the [documentation](https://dcsg.github.io/eventception/).
#### Events
When an event is dispatched, it's identified by a unique name, which any number of listeners might be listening to. An Event instance is also created and passed to all of the listeners. As you'll see later, the Event object itself often contains data about the event being dispatched.#### The Dispatcher
The dispatcher is the central object of the event dispatcher system.
In general, a single dispatcher is created, which maintains a registry of listeners.
When an event is dispatched via the dispatcher, it notifies all listeners registered with that event:```ruby
require 'eventception'dispatcher = Eventception::Dispatcher.new
```#### Adding Listeners
```ruby
require 'eventception'
class TodoListener
def on_creation(event)
puts "created a new to-do with title: '#{event.todo.title}'"
end
endlistener = TodoListener.new
dispatcher.add_listener(
event_name: 'todo.created',
listener: listener,
listener_method: 'on_creation'
)
```#### Creating and Dispatching an Event
##### Creating the Event
```ruby
require 'eventception'
class TodoCreatedEvent < Eventception::Event
NAME = 'todo.created'.freezeattr_reader :todo
def initialize(todo)
@todo = todo
end
end
```##### Dispatching the Event
```ruby
class Todo
attr_reader :titledef initialize(title:)
@title = title
end
endtodo = Todo.new('My To-do')
event = TodoCreatedEvent.new(todo)
dispatcher.dispatch(TodoCreatedEvent::NAME, event)# STDOUT: created a new to-do with title: 'My To-do'
```## LICENSE
Licensed under [GNU LGPLv3](https://github.com/dcsg/eventception/blob/master/LICENSE)
## Contributing
1. Fork it
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 new Pull Request## Contributors
* [Daniel Gomes](https://github.com/dcsg)
* [Ivo Anjo](https://github.com/ivoanjo)