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.
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.
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
License
The code is licensed under an MIT license. Check the LICENSE file for details.