Module: RubyTorrent::MinIntervalMethods

Defined in:
lib/rubytorrent/util.rb

Overview

ensure that a method doesn’t execute more frequently than some number of seconds. e.g.:

def meth

...

end min_iterval :meth, 10

ensures that “meth” won’t be executed more than once every 10 seconds.

Instance Method Summary collapse

Instance Method Details

#min_interval(meth, int) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rubytorrent/util.rb', line 132

def min_interval(meth, int)
  class_eval %{
    @@min_interval ||= {}
    @@min_interval[:#{meth}] = [nil, #{int.to_i}]
    alias :min_interval_#{meth} :#{meth}
    def #{meth}(*a, &b)
      last, int = @@min_interval[:#{meth}]
      unless last && ((Time.now - last) < int)
        min_interval_#{meth}(*a, &b) 
        @@min_interval[:#{meth}][0] = Time.now
      end
    end
  }
end