Style question: wrapping functionality around existing pkg

My application requires “Mechanize with a holdoff”, that is, one that
limits its #get and #submit requests to a maximum of one every N
seconds. I have a simple Governor class which runs a block with a
minimum holdoff between runs:

class Governor
def with_holdoff(holdoff)
unless (@ran_at.nil? || holdoff.nil?)
delay = (@ran_at + holdoff) - Time.now
sleep(delay) if (delay > 0)
end
@ran_at = Time.now
yield
end
end

I could write a class that encapsulates a Mechanize object and wraps a
with_holdoff call around its #get and #submit methods, e.g.:

class WrappedMechanize
@mechanize = Mechanize.new

def get(uri)
@governor.with_holdoff(@holdoff_time) { @mechanize.get(uri) }
end
end

… but that seems un-rubyish. What’s the cleanest way to extend the
Mechanize functionality? Perhaps a subclass like this?

class MechanizeWithHoldoff < Mechanize

def get_with_holdoff(uri, holdoff_time)
@governor.with_holdoff(holdoff_time) { super.get(uri) }
end
end

Suggestions welcome.

  • ff