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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(capacity, mode: :fifo) ⇒ 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
62
63
64
65
66
67
68
# File 'lib/aikido/zen/capped_collections.rb', line 57

def initialize(capacity, mode: :fifo)
  raise ArgumentError, "cannot set capacity lower than 1: #{capacity}" if capacity < 1

  unless [:fifo, :lru].include?(mode)
    raise ArgumentError, "unsupported mode: #{mode}"
  end

  @capacity = capacity
  @mode = mode

  @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) ⇒ 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.



75
76
77
78
# File 'lib/aikido/zen/capped_collections.rb', line 75

def [](key)
  @data[key] = @data.delete(key) if @mode == :lru && key?(key)
  @data[key]
end

#[]=(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.



70
71
72
73
# File 'lib/aikido/zen/capped_collections.rb', line 70

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

#fetch(key) ⇒ 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.



80
81
82
83
84
# File 'lib/aikido/zen/capped_collections.rb', line 80

def fetch(key, ...)
  return self[key] if key?(key)

  @data.fetch(key, ...)
end