Class: CoPilot::ArrayLimiter
- Inherits:
-
Object
- Object
- CoPilot::ArrayLimiter
- Defined in:
- lib/copilot/requests/internal/array_limiter.rb
Instance Attribute Summary collapse
-
#limit_reached_lambda ⇒ Object
Returns the value of attribute limit_reached_lambda.
-
#time_lambda ⇒ Object
Returns the value of attribute time_lambda.
Instance Method Summary collapse
- #add(value) ⇒ Object
-
#initialize(array, options = {}) ⇒ ArrayLimiter
constructor
A new instance of ArrayLimiter.
- #max_number_reached?(number) ⇒ Boolean
- #max_time_reached?(time) ⇒ Boolean
Constructor Details
#initialize(array, options = {}) ⇒ ArrayLimiter
Returns a new instance of ArrayLimiter.
6 7 8 9 10 11 12 |
# File 'lib/copilot/requests/internal/array_limiter.rb', line 6 def initialize(array, ={}) @array = array @max_number = [:max_number] @max_time = [:max_time] @time_lambda = lambda { |v| v[:time] } @limit_reached_lambda = {} end |
Instance Attribute Details
#limit_reached_lambda ⇒ Object
Returns the value of attribute limit_reached_lambda.
4 5 6 |
# File 'lib/copilot/requests/internal/array_limiter.rb', line 4 def limit_reached_lambda @limit_reached_lambda end |
#time_lambda ⇒ Object
Returns the value of attribute time_lambda.
4 5 6 |
# File 'lib/copilot/requests/internal/array_limiter.rb', line 4 def time_lambda @time_lambda end |
Instance Method Details
#add(value) ⇒ Object
14 15 16 17 18 |
# File 'lib/copilot/requests/internal/array_limiter.rb', line 14 def add(value) time = @time_lambda.call value limit_reached = max_number_reached?(@array.length) || max_time_reached?(time) limit_reached ? @limit_reached_lambda.call : @array << value end |
#max_number_reached?(number) ⇒ Boolean
20 21 22 |
# File 'lib/copilot/requests/internal/array_limiter.rb', line 20 def max_number_reached?(number) @max_number && number >= @max_number end |
#max_time_reached?(time) ⇒ Boolean
24 25 26 |
# File 'lib/copilot/requests/internal/array_limiter.rb', line 24 def max_time_reached?(time) @max_time && time > @max_time end |