Class: RunNoMo::Limiter

Inherits:
Object
  • Object
show all
Defined in:
lib/run_no_mo/limiter.rb

Instance Method Summary collapse

Constructor Details

#initialize(times:, within:, raises:) ⇒ Limiter

Returns a new instance of Limiter.



5
6
7
8
9
10
11
12
13
# 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

Instance Method Details

#run(&block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# 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