Class: Leakybucket::Bucket
- Inherits:
-
Object
- Object
- Leakybucket::Bucket
- Defined in:
- lib/leakybucket/bucket.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
Returns the value of attribute key.
-
#leaking_callback ⇒ Object
Returns the value of attribute leaking_callback.
-
#limit ⇒ Object
Returns the value of attribute limit.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #decrement ⇒ Object
- #default_options ⇒ Object
- #increment ⇒ Object
-
#initialize(options = {}, key = '') ⇒ Bucket
constructor
A new instance of Bucket.
- #leaking! ⇒ Object
- #leaking? ⇒ Boolean
- #reset ⇒ Object
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( = {}, key = '') self.limit = .merge()[:limit].to_i self.value = self.limit self.key = key end |
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
3 4 5 |
# File 'lib/leakybucket/bucket.rb', line 3 def key @key end |
#leaking_callback ⇒ Object
Returns the value of attribute leaking_callback.
3 4 5 |
# File 'lib/leakybucket/bucket.rb', line 3 def leaking_callback @leaking_callback end |
#limit ⇒ Object
Returns the value of attribute limit.
3 4 5 |
# File 'lib/leakybucket/bucket.rb', line 3 def limit @limit end |
#value ⇒ Object
Returns the value of attribute value.
3 4 5 |
# File 'lib/leakybucket/bucket.rb', line 3 def value @value end |
Instance Method Details
#decrement ⇒ Object
15 16 17 18 |
# File 'lib/leakybucket/bucket.rb', line 15 def decrement self.value -= 1 leaking! if leaking? end |
#default_options ⇒ Object
11 12 13 |
# File 'lib/leakybucket/bucket.rb', line 11 def {limit: 3} end |
#increment ⇒ Object
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
25 26 27 |
# File 'lib/leakybucket/bucket.rb', line 25 def leaking? value < 0 end |
#reset ⇒ Object
33 34 35 |
# File 'lib/leakybucket/bucket.rb', line 33 def reset self.value = limit end |