public
Description: auto expiring / auto refreshing attributes for all your ruby classes
Homepage: http://codeforpeople.rubyforge.org/expiration-date
Clone URL: git://github.com/TwP/expiration-date.git
name age message
file History.txt Mon Aug 25 09:53:12 -0700 2008 Added support for expring class variables. [TwP]
file Manifest.txt Thu Oct 16 14:57:25 -0700 2008 Now using the latests version of Mr Bones [TwP]
file README.rdoc Mon Aug 25 09:53:12 -0700 2008 Added support for expring class variables. [TwP]
file Rakefile Thu Oct 16 14:57:25 -0700 2008 Now using the latests version of Mr Bones [TwP]
directory lib/ Mon Aug 25 09:53:12 -0700 2008 Added support for expring class variables. [TwP]
directory spec/ Mon Aug 25 09:53:12 -0700 2008 Added support for expring class variables. [TwP]
README.rdoc

Expiration Date

  by Tim Pease
  http://codeforpeople.rubyforge.org/expiration-date

DESCRIPTION:

Really simple caching by attaching an expiration date to attributes.

The ExpirationDate module adss two methods to a class — expiring_attr and expiring_class_attr. These two methods are used to declare attributes in the instance and in the class, respectively, that will expire after some period of seconds have elapsed. The attribute is re-initialized from the given block after it has expired. This is a very simple form of caching.

SYNOPSIS:

A simple example demonstrating how the block gets called after the expiration time is passed.

  class A
    include ExpirationDate
    expiring_attr( :foo, 60 ) { 'foo' }
  end

  a = A.new
  a.foo                  #=> 'foo'
  a.foo = 'bar'
  a.foo                  #=> 'bar'

  sleep 61
  a.foo                  #=> 'foo'

A slightly more useful example. Here we are going to extract information from a database every five minutes. This assumes you have the ‘activesupport’ and ‘activerecord’ gems installed.

  class MyModel < ::ActiveRecord::Base
    include ExpirationDate
    expiring_class_attr( :costly_data, 5.minutes ) {
      models = MyModel.find( :all, :conditions => ['costly query conditions'] )
      result = models.map {|m| # costly operations here}
      result
    }
  end

  MyModel.costly_data    #=> result

Attributes can be expired manually, and the time it takes them to expire can be modified as well.

  class AgeDemo
    include ExpirationDate
    expiring_attr( :bar, 120 ) { Time.now }
  end

  demo = AgeDemo.new
  demo.bar               #=> now

  sleep 60
  demo.bar               #=> 60 seconds ago

  demo.expire_bar_now
  demo.bar               #=> now

  demo.alter_bar_age( 10 )
  demo.expire_bar_now
  demo.bar               #=> now
  sleep 11
  demo.bar               #=> now

REQUIREMENTS:

This is a pure ruby library. There are no requirements for using this code.

INSTALL:

  sudo gem install expiration-date

FUN STORY:

Wandering the grocery aisle the other day I saw a package of bacon with a neon green sticker screaming for all to hear "THIS BACON ONLY COSTS ONE DOLLAR!!!". Screaming bacon always intrigues me, so I grabbed the nearest store manager. He works at the coffee shop next door, but when I told him about the screaming bacon he had to come and see for himself.

"Oh, it’s a manager’s special" he told me. "When products get too old the manager will reduce the price so they sell more quickly. This allows new products to be put on the shelves, and the store can make some money instead of throwing out the expired products".

I stared at him blankly. He wandered back to the coffee shop and had a latte.

I continued to stand there thinking about expiring products and how Ruby could benefit from neon green stickers and stale bacon. Eventually the grocery store manager came by and asked me if everything was okay. I grabbed him by the collar, pointed at the bacon and yelled "DO YOU KNOW WHAT THIS MEANS!?!?". I ran from the store, grabbed my laptop, and whipped up this little gem.

EXPIRATION DATE (now with more neon green).

Now ruby can expire it’s bacon, too, just like the grocery store, and make room for more bacon from the delivery truck.

LICENSE:

The MIT License

Copyright © 2008

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.