github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

maxim / stalker

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 3
    • 0
  • Source
  • Commits
  • Network (0)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Rails micro-plugin which allows models to observe each other. — Read more

  cancel

http://github.com/maxim/stalker

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Add should_stalk shoulda macros. Make after_initialize/after_find 
exceptions. 
maxim (author)
Mon Apr 27 19:28:02 -0700 2009
commit  5ef0d198eb3c3e72839843bbbf990fa6d2b57454
tree    c2458aaa9708fcd28ab28c1395da75225d50dbaf
parent  4869bbb69ce51233031a904d32f9cf291dc2b6bb
stalker /
name age
history
message
file README.rdoc Loading commit data...
file init.rb
directory lib/
directory shoulda_macros/
README.rdoc

Stalker

Rails micro-plugin (38 lines) which allows models to observe each other.

Install

  script/plugin install git://github.com/maxim/stalker.git

Usage

Simple.

  class Celebrity < ActiveRecord::Base
  end

  class Idiot < ActiveRecord::Base
    stalk Celebrity

    class << self
      def before_celebrity_save(celebrity)
        puts "Idiot: 'Click'. Haha, got your picture!"
      end
    end
  end

Just like with observers, you have to edit environment.rb to tell rails to load Idiot first. (Idiot should be on the scene when Celebrity shows up.)

  config.active_record.observers = [:idiot]

Now let’s see what happens.

  % britney = Celebrity.new
  % britney.save
  % Idiot: 'Click'. Haha, got your picture!

What happened?

The stalker class (Idiot) now has magical class methods (such as before_[stalked_class]_save(stalked_instance), after_[stalked_class]_validate(stalked_instance), etc) for reacting to stalked class’s callbacks. Keep in mind they’re class methods, hence the class << self thingie. As an argument these methods get the reference to the instance of the stalked class. This way you can alter the object or just extract information from it.

Group your victims!

If you want to stalk multiple models with same responses, it’s easy to do using :group option.

  class Celebrity < ActiveRecord::Base
  end

  class Politician < ActiveRecord::Base
  end

  class Idiot < ActiveRecord::Base
    stalk Celebrity, Politician, :group => :victim

    def self.before_victim_save(victim)
      if victim.is_a?(Politician)
        poke(victim)
      else
        bite(victim)
      end
    end
  end

Why?

Sometimes you want model A to notify model B if something happens to it, so that model B could react. It’s useful if it’s concern of model B, like it has to update its records. If you have Product model that needs to decrement count of products when Order has been placed, you’d want Order model to notify Product model about the fact that it’s been placed. Product can then decrement item count in itself.

Can Idiot prevent Celebrity from saving?

Yes. If Idiot defines method before_celebrity_save and return false - it will halt the callback chain for Celebrity as usual.

But rails already has observers!

They’re useful when you want to have separate class for reacting to certain model’s callbacks. What if you want other existing models to react? That’s extra manual work that could be eliminated.

TODO

Write tests!!!

License

Copyright © 2009 Maxim Chernyak

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server