class RunNoMo::Limiter

Public Class Methods

new(times:, within:, raises:) click to toggle source
# File lib/run_no_mo/limiter.rb, line 5
def initialize(times:, within:, raises:)
  @times = times
  @count = 0

  @within = within
  @last_run = nil

  @raises = raises
end

Public Instance Methods

run(&block) click to toggle source
# File lib/run_no_mo/limiter.rb, line 15
def run(&block)
  if reset_counter?
    @count = 0
  else
    @count+= 1
  end

  @last_run = Time.now

  return limit_exceeded! if @count > @times
  
  block.call
end

Private Instance Methods

limit_exceeded!() click to toggle source
# File lib/run_no_mo/limiter.rb, line 31
def limit_exceeded!
  raise LimitExceeded.new if @raises
end
reset_counter?() click to toggle source
# File lib/run_no_mo/limiter.rb, line 35
def reset_counter?
  return false if @within.nil?

  return false if @last_run.nil?

  return false if (Time.now - @last_run) < @within

  true
end