Class: Whisper::TimedMap

Inherits:
Object
  • Object
show all
Includes:
Loggy
Defined in:
lib/whisper/timed_map.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTimedMap

Returns a new instance of TimedMap.



10
11
12
13
# File 'lib/whisper/timed_map.rb', line 10

def initialize
  @h = {}
  TimedMap.expiration_interval ||= 60
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a) ⇒ Object



40
# File 'lib/whisper/timed_map.rb', line 40

def method_missing(m, *a); @h.send(m, *a) end

Class Attribute Details

.expiration_intervalObject

Returns the value of attribute expiration_interval.



7
8
9
# File 'lib/whisper/timed_map.rb', line 7

def expiration_interval
  @expiration_interval
end

Instance Method Details

#[](k) ⇒ Object



19
20
21
# File 'lib/whisper/timed_map.rb', line 19

def [] k
  @h[k] && @h[k][0]
end

#[]=(k, v) ⇒ Object



15
16
17
# File 'lib/whisper/timed_map.rb', line 15

def []= k, v
  @h[k] = [v, Time.now]; v
end

#eachObject



23
24
25
# File 'lib/whisper/timed_map.rb', line 23

def each
  @h.each { |k, (a, b)| yield k, a }
end

#prune!Object



31
32
33
34
35
36
37
38
# File 'lib/whisper/timed_map.rb', line 31

def prune!
  @h.each do |k, v|
    if (v[1] + TimedMap.expiration_interval) < Time.now
      debug "expiring #{k.inspect} because it was generated more than #{TimedMap.expiration_interval}s ago"
      @h.delete k 
    end
  end
end

#valuesObject



27
28
29
# File 'lib/whisper/timed_map.rb', line 27

def values
  @h.values.map { |a, b| a }
end