Git Product home page Git Product logo

event_emitter's Introduction

event_emitter

Build Status

Install

% gem install event_emitter

Requirements

testing on

  • Ruby 1.8.7
  • Ruby 1.9.2
  • Ruby 2.0.0
  • Ruby 2.1.0
  • JRuby

Synopsys

load rubygem

require "rubygems"
require "event_emitter"

include

class User
  include EventEmitter
  attr_accessor :name
end

register event listener

user = User.new
user.name = "shokai"
user.on :go do |data|
  puts "#{name} go to #{data[:place]}"
end

call event

user.emit :go, {:place => "mountain"}
# => "shokai go to mountain"

regist event using "once"

user.once :eat do |what, where|
  puts "#{name} -> eat #{what} at #{where}"
end

call

user.emit :eat, "BEEF", "zanmai"  # =>  "shokai -> eat BEEF at zanmai"
user.emit :eat, "Ramen", "marutomo"  # => do not call. call only first time.

apply as instance-specific method

class Foo
end

foo = Foo.new
EventEmitter.apply foo

remove event listener

user.remove_listener :go
user.remove_listener event_id

catch all events

user.on :* do |event_name, args|
  puts event_name + " called"
  p args
end

listen to all instances of a class

User.instance_on :eat do |what, where|
  puts "#{name} -> eat #{what} at #{where}"
end

call

user = User.new
user.name = "shokai"
user.emit :eat, "BEEF", "zanmai"  # =>  "shokai -> eat BEEF at zanmai"

see samples https://github.com/shokai/event_emitter/tree/master/samples

Test

% gem install bundler
% bundle install
% rake test

Benchmark

% rake benchmark

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

event_emitter's People

Contributors

shokai avatar treygriffith 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.