# Expiration-date 1.0.0 (Stale Green Bacon)

**URL:** <https://www.ruby-forum.com/t/expiration-date-1-0-0-stale-green-bacon/146599>\
**Category:** Ruby\
**Created:** [August 22, 2008, 9:28pm UTC](https://www.ruby-forum.com/t/expiration-date-1-0-0-stale-green-bacon/146599 "2008-08-22T21:28:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 22, 2008, 9:28pm UTC](https://www.ruby-forum.com/t/expiration-date-1-0-0-stale-green-bacon/146599/1 "2008-08-22T21:28:35Z")

</div>

expiration-date version 1.0.0  
by Tim P.  
[http://codeforpeople.rubyforge.org/expiration-date](http://codeforpeople.rubyforge.org/expiration-date)  
(the “Stale Green Bacon” release)

== DESCRIPTION

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.

== CHANGES

- 1 major enhancement
  - Birthday!

== SYNOPSIS

The ExpirationDate module adds the “expiring\_attr” method to a class.  
This  
method is used to define an attribute that will expire after some period  
of  
seconds have elapsed.

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.object\_id #=\> 123456  
a.foo.object\_id #=\> 123456

sleep 61  
a.foo.object\_id #=\> 654321

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\_attr( :costly\_data, 5.minutes ) {  
models = MyModel.find( :all, :conditions =\> [‘costly query  
conditions’] )  
result = models.map {|m| # costly operations here}  
result  
}  
end

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\_now(:bar)  
demo.bar #=\> now

demo.alter\_expiration\_label(:bar, 10)  
demo.expire\_now(:bar)  
demo.bar #=\> now  
sleep 11  
demo.bar #=\> now

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 23, 2008, 8:11am UTC](https://www.ruby-forum.com/t/expiration-date-1-0-0-stale-green-bacon/146599/2 "2008-08-23T08:11:36Z")

</div>

removed\_email\_address@domain.invalid wrote:

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

That was just awesome.

-Dana
