Giter Site home page Giter Site logo

observational's Introduction

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 © 2009 James Golick. See LICENSE for details.

observational's People

Contributors

jamesgolick avatar lapluviosilla avatar

Stargazers

 avatar

Watchers

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