public
Description: Use the observer pattern to better divide your objects' responsibilities.
Homepage:
Clone URL: git://github.com/giraffesoft/observational.git
name age message
file .gitignore Wed Aug 05 11:01:19 -0700 2009 setup YARD rake task [giraffesoft]
file LICENSE Thu Jul 16 07:40:10 -0700 2009 Initial commit to observational. [giraffesoft]
file README.rdoc Wed Aug 05 11:03:27 -0700 2009 add info about finding the docs at docs.github [giraffesoft]
file Rakefile Wed Aug 05 11:01:19 -0700 2009 setup YARD rake task [giraffesoft]
file VERSION Wed Aug 05 13:31:15 -0700 2009 Version bump to 0.2.3 [giraffesoft]
directory lib/ Wed Aug 05 13:07:23 -0700 2009 quick rename [giraffesoft]
file observational.gemspec Wed Aug 05 13:31:17 -0700 2009 Regenerated gemspec for version 0.2.3 [giraffesoft]
directory rails/ Wed Aug 05 13:30:57 -0700 2009 include observable in all activerecord models [giraffesoft]
directory spec/ Wed Aug 05 13:07:23 -0700 2009 quick rename [giraffesoft]
README.rdoc

Observational

How many times have you seen this in a rails app?

  class User
    after_create :deliver_welcome_message

    protected
    def deliver_welcome_message
      Notifier.deliver_welcome_message(self)
    end
  end

Why is the user concerned with the delivery of his own welcome message? It seems like the Notifier should be responsible for that.

Observational makes it possible to make it the Notifier’s responsibility, using the observer pattern.

The equivalent of the above example is:

  class Notifier < ActionMailer::Base
    observes :user, :invokes => :deliver_welcome_message, :after => :create

    def welcome_message(user)
      # do mailer stuff here
    end
  end

After a user is created, Notifier.deliver_welcome_message(that_user) will be invoked.

It’s also possible to specify that the observer method gets called with a specific attribute from the observed object.

  class Creditor
    observes :message, :invokes => :use_credit, :with => :creator, :after => :create

    def use_credit(user)
      # do something
    end
  end

After a message is created, Creditor.use_credit(message.creator) will be called.

Observational supports all of ActiveRecord’s callbacks.

YARDOC

Observational uses YARD, because it’s a million times better than RDoc. You can find the docs at docs.github.com/giraffesoft/observational

General Purpose Observers

Observational can also be used to add observers to ruby classes that aren’t related to active_record. But, that’s not documented yet :-).

Copyright

Copyright © 2009 James Golick. See LICENSE for details.