# Style question: wrapping functionality around existing pkg

**URL:** <https://www.ruby-forum.com/t/style-question-wrapping-functionality-around-existing-pkg/191066>\
**Category:** Rails\
**Created:** [July 4, 2010, 4:25am UTC](https://www.ruby-forum.com/t/style-question-wrapping-functionality-around-existing-pkg/191066 "2010-07-04T04:25:24Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [July 4, 2010, 4:25am UTC](https://www.ruby-forum.com/t/style-question-wrapping-functionality-around-existing-pkg/191066/1 "2010-07-04T04:25:24Z")

</div>

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
