Giter Club home page Giter Club logo

rbminivents's Introduction

rbminivents

This gem allows you to easily use minimalistic events in Ruby.

rb.minvents logo

Description

This gem was made under the influence of the tiny JS library minivents by 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:

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

require 'rbminivents'

sandbox = RbMinivents::Events.new

sandbox.on(:event) do
  # do stuff
end

sandbox.emit(:event)  # does stuff

sandbox.off(:event)

sandbox.emit(:event)  # does not do stuff

Passing parameters

require 'rbminivents'

sandbox = RbMinivents::Events.new

sandbox.on(:event) do |name|
  puts "Hello, #{name}!"
end

sandbox.emit(:event, 'Max')  #=> Hello, Max!
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

require 'rbminivents'

sandbox = RbMinivents::Events.new

handler1 = sandbox.on(:event) do
  puts "First Hello!"
end

handler2 = 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

rbminivents's People

Contributors

maxbarsukov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.