Module: Solargraph::Cache

Defined in:
lib/solargraph/cache.rb

Class Method Summary collapse

Class Method Details

.base_dirString

The base directory where cached documentation is installed.

Returns:

  • (String)


9
10
11
12
13
# File 'lib/solargraph/cache.rb', line 9

def base_dir
  # The directory is not stored in a variable so it can be overridden
  # in specs.
  ENV['SOLARGRAPH_CACHE'] || File.join(Dir.home, '.solargraph', 'cache')
end

.clearObject



46
47
48
# File 'lib/solargraph/cache.rb', line 46

def clear
  FileUtils.rm_rf base_dir, secure: true
end

.load(*path) ⇒ Array<Solargraph::Pin::Base>?

Returns:



25
26
27
28
29
30
31
32
33
# File 'lib/solargraph/cache.rb', line 25

def load *path
  file = File.join(work_dir, *path)
  return nil unless File.file?(file)
  Marshal.load(File.read(file, mode: 'rb'))
rescue StandardError => e
  Solargraph.logger.warn "Failed to load cached file #{file}: [#{e.class}] #{e.message}"
  FileUtils.rm_f file
  nil
end

.save(*path, pins) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
# File 'lib/solargraph/cache.rb', line 36

def save *path, pins
  return false if pins.empty?
  file = File.join(work_dir, *path)
  base = File.dirname(file)
  FileUtils.mkdir_p base unless File.directory?(base)
  ser = Marshal.dump(pins)
  File.write file, ser, mode: 'wb'
  true
end

.work_dirString

The working directory for the current Ruby and Solargraph versions.

Returns:

  • (String)


18
19
20
21
22
# File 'lib/solargraph/cache.rb', line 18

def work_dir
  # The directory is not stored in a variable so it can be overridden
  # in specs.
  File.join(base_dir, "ruby-#{RUBY_VERSION}", "rbs-#{RBS::VERSION}", "solargraph-#{Solargraph::VERSION}")
end