Class: Smith::Tool::CallAllowanceCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/smith/tool/call_allowance_counter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(budget) ⇒ CallAllowanceCounter

Returns a new instance of CallAllowanceCounter.



6
7
8
9
10
# File 'lib/smith/tool/call_allowance_counter.rb', line 6

def initialize(budget)
  @budget = budget
  @remaining = budget.total
  @remaining_by_tool = budget.tool_limits&.dup
end

Instance Attribute Details

#remainingObject (readonly)

Returns the value of attribute remaining.



12
13
14
# File 'lib/smith/tool/call_allowance_counter.rb', line 12

def remaining
  @remaining
end

Instance Method Details

#available?(batch) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/smith/tool/call_allowance_counter.rb', line 22

def available?(batch)
  remaining >= batch.size && tool_counts_available?(batch.counts)
end

#consume!(batch) ⇒ Object



26
27
28
29
30
31
# File 'lib/smith/tool/call_allowance_counter.rb', line 26

def consume!(batch)
  @remaining -= batch.size
  return unless @budget.exact?

  batch.counts.each { |name, count| @remaining_by_tool[name] -= count }
end

#remaining_for(tool_name) ⇒ Object



14
15
16
# File 'lib/smith/tool/call_allowance_counter.rb', line 14

def remaining_for(tool_name)
  @remaining_by_tool.fetch(tool_name, 0)
end

#used?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/smith/tool/call_allowance_counter.rb', line 18

def used?
  remaining < @budget.total
end