Class: Leakybucket::Bucket

Inherits:
Object
  • Object
show all
Defined in:
lib/leakybucket/bucket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, key = '') ⇒ Bucket

Returns a new instance of Bucket.



5
6
7
8
9
# File 'lib/leakybucket/bucket.rb', line 5

def initialize(options = {}, key = '')
  self.limit = default_options.merge(options)[:limit].to_i
  self.value = self.limit
  self.key = key
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



3
4
5
# File 'lib/leakybucket/bucket.rb', line 3

def key
  @key
end

#leaking_callbackObject

Returns the value of attribute leaking_callback.



3
4
5
# File 'lib/leakybucket/bucket.rb', line 3

def leaking_callback
  @leaking_callback
end

#limitObject

Returns the value of attribute limit.



3
4
5
# File 'lib/leakybucket/bucket.rb', line 3

def limit
  @limit
end

#valueObject

Returns the value of attribute value.



3
4
5
# File 'lib/leakybucket/bucket.rb', line 3

def value
  @value
end

Instance Method Details

#decrementObject



15
16
17
18
# File 'lib/leakybucket/bucket.rb', line 15

def decrement
  self.value -= 1
  leaking! if leaking?
end

#default_optionsObject



11
12
13
# File 'lib/leakybucket/bucket.rb', line 11

def default_options
  {limit: 3}
end

#incrementObject



20
21
22
23
# File 'lib/leakybucket/bucket.rb', line 20

def increment
  return if value >= limit
  self.value += 1
end

#leaking!Object



29
30
31
# File 'lib/leakybucket/bucket.rb', line 29

def leaking!
  leaking_callback.(value) if leaking_callback.respond_to?(:call)
end

#leaking?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/leakybucket/bucket.rb', line 25

def leaking?
  value < 0
end

#resetObject



33
34
35
# File 'lib/leakybucket/bucket.rb', line 33

def reset
  self.value = limit
end