Class: Memory::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/memory/cache.rb

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



15
16
17
18
19
20
# File 'lib/memory/cache.rb', line 15

def initialize
	@gem_guess_cache = Hash.new
	@location_cache = Hash.new { |h, k| h[k] = Hash.new.compare_by_identity }
	@class_name_cache = Hash.new.compare_by_identity
	@string_cache = Hash.new
end

Instance Method Details

#guess_gem(path) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/memory/cache.rb', line 22

def guess_gem(path)
	@gem_guess_cache[path] ||=
		if /(\/gems\/.*)*\/gems\/(?<gemname>[^\/]+)/ =~ path
			gemname
		elsif /\/rubygems[\.\/]/ =~ path
			"rubygems"
		elsif /ruby\/2\.[^\/]+\/(?<stdlib>[^\/\.]+)/ =~ path
			stdlib
		elsif /(?<app>[^\/]+\/(bin|app|lib))/ =~ path
			app
		else
			"other"
		end
end

#lookup_class_name(klass) ⇒ Object



41
42
43
# File 'lib/memory/cache.rb', line 41

def lookup_class_name(klass)
	@class_name_cache[klass] ||= ((klass.is_a?(Class) && klass.name) || '<<Unknown>>').to_s
end

#lookup_location(file, line) ⇒ Object



37
38
39
# File 'lib/memory/cache.rb', line 37

def lookup_location(file, line)
	@location_cache[file][line] ||= "#{file}:#{line}"
end

#lookup_string(obj) ⇒ Object



45
46
47
48
49
50
# File 'lib/memory/cache.rb', line 45

def lookup_string(obj)
	# This string is shortened to 200 characters which is what the string report shows
	# The string report can still list unique strings longer than 200 characters
	#   separately because the object_id of the shortened string will be different
	@string_cache[obj] ||= String.new << obj[0, 64]
end