OVERVIEW

Project Observe
Homepage https://github.com/robgleeson/observe
Documentation http://rubydoc.info/gems/observe/frames
Author Rob Gleeson

DESCRIPTION

A simple interface for adding observers to any class. The interface is similar
to and takes ideas from the 'Observable' module in the Ruby standard library.

DIFFERENCES

There are a few differences between 'Observe' and the 'Observable' module:

  • An observer is any object that responds to call.
    Proc objects can be observers out of the box.

  • Observers belong to 'groups'.
    In the example below, :ignition is a group, as is :brakes.

  • There is no changed method.
    If you want to notify observers, go ahead & use notify_observers.
    If there is state that needs to be true before observers can be notified,
    you can use if(…).

FULL DISCLOSURE

It can help to know what methods and state is introduced by Observe so your
class or module can avoid stepping on the toes of Observe by accident. When
you include Observe into a class or module it adds four methods:

  • add_observer
    Adds an observer for the including class/module.

  • notify_observers
    Notify observers registered for the including class/module.

  • destroy_observers
    Destroy observers registered for the incluing class/module.

  • observers
    You shouldn't need to use this method.
    It is used internally by the above methods.

It adds the following instance variable:

  • @observers
    Nothing special, a list of observers used by the 'observers' method.

EXAMPLE

class Car

  include Observe

  def initialize
    add_observer(:ignition) { .. }  
    add_observer(:brakes) { .. } 
  end

  def drive
    notify_observers(:ignition)
    puts "Vroom Vroom."
    notify_observers(:brakes)
  end

end

Car.new.drive

INSTALL

gem install observe

LICENSE

See LICENSE.txt