Class: DeadCodeDetector::Storage::MemoryBackend

Inherits:
Object
  • Object
show all
Defined in:
lib/dead_code_detector/storage/memory_backend.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMemoryBackend

Returns a new instance of MemoryBackend.



9
10
11
12
# File 'lib/dead_code_detector/storage/memory_backend.rb', line 9

def initialize
  @mapping = Hash.new{|h,k| h[k] = Set.new }
  @pending_deletions = Hash.new{|h,k| h[k] = Set.new }
end

Instance Attribute Details

#flush_immediatelyObject

Returns the value of attribute flush_immediately.



6
7
8
# File 'lib/dead_code_detector/storage/memory_backend.rb', line 6

def flush_immediately
  @flush_immediately
end

#pending_deletionsObject (readonly)

Returns the value of attribute pending_deletions.



7
8
9
# File 'lib/dead_code_detector/storage/memory_backend.rb', line 7

def pending_deletions
  @pending_deletions
end

Instance Method Details

#add(key, values) ⇒ Object



18
19
20
# File 'lib/dead_code_detector/storage/memory_backend.rb', line 18

def add(key, values)
  @mapping[key].merge(Array(values))
end

#clear(key) ⇒ Object



14
15
16
# File 'lib/dead_code_detector/storage/memory_backend.rb', line 14

def clear(key)
  @mapping.delete(key)
end

#delete(key, value) ⇒ Object



30
31
32
33
# File 'lib/dead_code_detector/storage/memory_backend.rb', line 30

def delete(key, value)
  @pending_deletions[key] << value.to_s
  flush if flush_immediately
end

#flushObject



35
36
37
38
39
40
# File 'lib/dead_code_detector/storage/memory_backend.rb', line 35

def flush
  @pending_deletions.each do |key, values|
    @mapping[key].subtract(values)
  end
  @pending_deletions.clear
end

#get(key) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/dead_code_detector/storage/memory_backend.rb', line 22

def get(key)
  if @pending_deletions.key?(key)
    @mapping[key] - @pending_deletions[key]
  else
    @mapping[key]
  end
end