Coder Social home page Coder Social logo

beacon's Introduction

Beacon

Simple observers and notifiers for your code.

The library aims to provide the simplest implementation possible. You get beacons, which are always global and can be “lit up”. Whenever you light a beacon up, everyone watching will act as instructed.

<img src=“https://secure.travis-ci.org/foca/beacon.png?branch=master” alt=“Build Status” />

How to use this?

Whenever you want to fire an event from your code, call Beacon.fire:

def do_something
  Beacon.fire(:before_doing_something)
  # do your thing
end

In order to register listeners for that event, call Beacon.watch:

Beacon.watch :before_doing_something do
  logger.info "I'm about to do something"
end

Each time you call watch with a given name, you register a different handler for that name. Each time you call fire all handlers for that event are run, in the order they were registered.

You can use on as an alias of watch, and trigger as an alias of fire, since those seem to be popular choices among event observing libraries.

Passing arguments

If you want to pass arguments to the watchers, just pass them along in Beacon.fire:

Beacon.fire(:an_event, "cuack", 3)

And you’ll get them as arguments on the block that handles the message:

Beacon.watch :an_event do |object, index|
  # here object == "cuack", index == 3
end

Advanced handlers

Instead of blocks, Beacon.watch can receive any object that responds to call, so if you need any advanced logic in your handlers, you can declare them like this:

class MyHandler
  def call(foo, bar=0)
    puts foo.inspect, bar
  end
end

Beacon.watch :an_event, MyHandler.new

Isolated events

You can include Beacon into your objects to have isolated events on a per-object basis if you need that:

class MyObservable
  include Beacon
end

obj = MyObservable.new
obj.watch(:an_event) { |object, *args| puts *args }
obj.trigger(:an_event, "foo")

Installing with rubygems

gem install beacon

Contributing

Clone our git repo from git://github.com/foca/beacon.git. The preffered way to send a patch is to push to a clone of the repo on github but if you want to mail me a patch or point me to a diff I won’t complain. Though I’ll probably take more to apply it :)

Authors

Code written by foca, with the help and ideas of halorgium, and tizoc.

The code is licensed under an MIT license. Check the LICENSE file for details.

beacon's People

Contributors

foca avatar halorgium avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

beacon's Issues

No tests

I haven't found the right way to write tests for this. If anyone is interested and comes up with some clear way to test the functionality then a patch is more than welcome :)

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.