Class: AllMyCircuits::Strategies::AbstractWindowStrategy::Window
- Inherits:
-
Object
- Object
- AllMyCircuits::Strategies::AbstractWindowStrategy::Window
- Defined in:
- lib/all_my_circuits/strategies/abstract_window_strategy/window.rb
Instance Method Summary collapse
- #<<(event) ⇒ Object
- #count(event = nil) ⇒ Object
- #full? ⇒ Boolean
-
#initialize(number_of_events) ⇒ Window
constructor
A new instance of Window.
- #inspect ⇒ Object
- #reset! ⇒ Object
Constructor Details
#initialize(number_of_events) ⇒ Window
Returns a new instance of Window.
6 7 8 9 10 11 12 13 |
# 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 |
Instance Method Details
#<<(event) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# 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) ⇒ Object
30 31 32 |
# 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? ⇒ Boolean
34 35 36 |
# File 'lib/all_my_circuits/strategies/abstract_window_strategy/window.rb', line 34 def full? @window.length == @number_of_events end |
#inspect ⇒ Object
38 39 40 |
# 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! ⇒ Object
15 16 17 18 |
# 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 |