Expiration-date 1.0.0 (Stale Green Bacon)

expiration-date version 1.0.0
by Tim P.
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

[email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs