class AllMyCircuits::Strategies::AbstractWindowStrategy::Window
Public Class Methods
new(number_of_events)
click to toggle source
# File lib/all_my_circuits/strategies/abstract_window_strategy/window.rb, line 6 def initialize(number_of_events) number_of_events = Integer(number_of_events) unless number_of_events > 0 raise ArgumentError, "window size must be a natural number" end @number_of_events = number_of_events reset! end
Public Instance Methods
<<(event)
click to toggle source
# File lib/all_my_circuits/strategies/abstract_window_strategy/window.rb, line 20 def <<(event) if full? event_to_decrement = @window.shift @counters[event_to_decrement] -= 1 end @window.push(event) @counters[event] += 1 self end
count(event = nil)
click to toggle source
# File lib/all_my_circuits/strategies/abstract_window_strategy/window.rb, line 30 def count(event = nil) event.nil? ? @window.length : @counters[event] end
full?()
click to toggle source
# File lib/all_my_circuits/strategies/abstract_window_strategy/window.rb, line 34 def full? @window.length == @number_of_events end
inspect()
click to toggle source
# File lib/all_my_circuits/strategies/abstract_window_strategy/window.rb, line 38 def inspect "#<%s:0x%x size: %d, counts: %s, full: %s" % [self.class.name, object_id, @number_of_events, @counters, full?] end
reset!()
click to toggle source
# File lib/all_my_circuits/strategies/abstract_window_strategy/window.rb, line 15 def reset! @window = [] @counters = Hash.new { |h, k| h[k] = 0 } end