Class: CoPilot::ArrayLimiter

Inherits:
Object
  • Object
show all
Defined in:
lib/copilot/requests/internal/array_limiter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options={})
  @array = array
  @max_number = options[:max_number]
  @max_time = options[:max_time]
  @time_lambda = lambda { |v| v[:time] }
  @limit_reached_lambda = {}
end

Instance Attribute Details

#limit_reached_lambdaObject

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_lambdaObject

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

Returns:

  • (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

Returns:

  • (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