https://github.com/maxbarsukov/rbminivents
📆 Tiny eventing gem
https://github.com/maxbarsukov/rbminivents
events gem gems ruby ruby-events ruby-gem
Last synced: about 1 month ago
JSON representation
📆 Tiny eventing gem
- Host: GitHub
- URL: https://github.com/maxbarsukov/rbminivents
- Owner: maxbarsukov
- License: mit
- Created: 2021-04-12T16:22:05.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-05-01T09:01:37.000Z (about 3 years ago)
- Last Synced: 2024-04-26T13:47:18.219Z (about 1 year ago)
- Topics: events, gem, gems, ruby, ruby-events, ruby-gem
- Language: Ruby
- Homepage:
- Size: 50.8 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# rbminivents
> This gem allows you to easily use minimalistic events in Ruby.

## Description
This gem was made under the influence of the tiny JS library [minivents](https://github.com/allouis/minivents) by [allouis](https://github.com/allouis) that I liked.
It seemed to me that it would be quite nice to move something like this to Ruby.## Getting started
### Requirements
This gem requires Ruby 2.5+. It might work with older versions of Rails though.
### Installation
Add the gem to your `Gemfile`:
```ruby
gem 'rbminivents'
```and run:
```
bundle install
```## API
This project is similar to the original one in many ways, but some features are not implemented. You can use standard ruby mixins for fully functionality. So,`on`: Listen to event. Returns handle
`off`: Stop listening to event
`emit`: Emit event
## Examples
### Standard way
```ruby
require 'rbminivents'sandbox = RbMinivents::Events.new
sandbox.on(:event) do
# do stuff
endsandbox.emit(:event) # does stuff
sandbox.off(:event)
sandbox.emit(:event) # does not do stuff
```### Passing parameters
```ruby
require 'rbminivents'sandbox = RbMinivents::Events.new
sandbox.on(:event) do |name|
puts "Hello, #{name}!"
endsandbox.emit(:event, 'Max') #=> Hello, Max!
``````ruby
require 'rbminivents'sandbox = RbMinivents::Events.new
sandbox.on(:event) do |hash, arr, num, bool|
puts hash[:name]
p arr
puts "5 + #{num} = #{5 + num}"
puts "It's true!" if bool
end# max, [1, 2, 3], 5 + 7 = 12
sandbox.emit(:event,
{name: 'max'},
[1, 2, 3],
7,
false
)
```### Using Off
```ruby
require 'rbminivents'sandbox = RbMinivents::Events.new
handler1 = sandbox.on(:event) do
puts "First Hello!"
endhandler2 = sandbox.on(:event) do
puts "Second Hello!"
end# remove just this callback
sandbox.emit(:event) # => First Hello!\nSecond Hello!sandbox.off(:event, handler1)
sandbox.emit(:event) # => Second Hello!sandbox.off(:event, handler2)
sandbox.emit(:event) # => nothing...# re-add listener to show can remove all at once
sandbox.on(:event) do
puts "Hello!"
end# remove all
sandbox.off(:event)
sandbox.emit(:event) # => nothing again
```