Class: Aikido::Zen::CappedMap Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/aikido/zen/capped_collections.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Provides a FIFO hash-like structure with a maximum size. Adding a new key after the capacity has been reached kicks the first element pair added out.

Direct Known Subclasses

Aikido::Zen::Collector::Users

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(capacity) ⇒ CappedMap

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of CappedMap.

Raises:

  • (ArgumentError)


57
58
59
60
61
# File 'lib/aikido/zen/capped_collections.rb', line 57

def initialize(capacity)
  raise ArgumentError, "cannot set capacity lower than 1: #{capacity}" if capacity < 1
  @capacity = capacity
  @data = {}
end

Instance Attribute Details

#capacityInteger (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Integer)


55
56
57
# File 'lib/aikido/zen/capped_collections.rb', line 55

def capacity
  @capacity
end

Instance Method Details

#[]=(key, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



63
64
65
66
# File 'lib/aikido/zen/capped_collections.rb', line 63

def []=(key, value)
  @data[key] = value
  @data.delete(@data.each_key.first) if @data.size > @capacity
end