Class: Solargraph::Collection::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/collection/base.rb

Overview

The base class for cacheable pin collections. Subclasses need to implement #pins and #cache_file.

Direct Known Subclasses

Core, Gem, Stdlib

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load ⇒ Array<Pin::Base>

Returns:



25
26
27
# File 'lib/solargraph/collection/base.rb', line 25

def self.load(...)
  new(...).load
end

.uncache ⇒ Object



29
30
31
32
33
# File 'lib/solargraph/collection/base.rb', line 29

def self.uncache(...)
  cache_file = new(...).cache_file
  FileUtils.rm_rf cache_file
  Collection.mem_cache.delete cache_file
end

Instance Method Details

#cache_file ⇒ String

This method is abstract.

The file that will store serialized pins.

Returns:

  • (String)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/solargraph/collection/base.rb', line 18

class Base
  # @return [Array<Pin::Base>]
  def load
    Collection.cached(cache_file) { file_cache_pins || generate_pin_caches }
  end

  # @return [Array<Pin::Base>]
  def self.load(...)
    new(...).load
  end

  def self.uncache(...)
    cache_file = new(...).cache_file
    FileUtils.rm_rf cache_file
    Collection.mem_cache.delete cache_file
  end

  private

  def file_cache_pins
    Marshal.load(File.read(cache_file, mode: 'rb')) if File.exist?(cache_file) # rubocop:disable Security/MarshalLoad
  end

  def generate_pin_caches
    Collection.mem_cache[cache_file] = pins
    serial = Marshal.dump(pins)
    FileUtils.mkdir_p File.dirname(cache_file)
    File.write cache_file, serial, mode: 'wb'
    pins
  end
end

#load ⇒ Array<Pin::Base>

Returns:



20
21
22
# File 'lib/solargraph/collection/base.rb', line 20

def load
  Collection.cached(cache_file) { file_cache_pins || generate_pin_caches }
end

#pins ⇒ Array<Pin::Base>

This method is abstract.

The array of pins for this collection.

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/solargraph/collection/base.rb', line 18

class Base
  # @return [Array<Pin::Base>]
  def load
    Collection.cached(cache_file) { file_cache_pins || generate_pin_caches }
  end

  # @return [Array<Pin::Base>]
  def self.load(...)
    new(...).load
  end

  def self.uncache(...)
    cache_file = new(...).cache_file
    FileUtils.rm_rf cache_file
    Collection.mem_cache.delete cache_file
  end

  private

  def file_cache_pins
    Marshal.load(File.read(cache_file, mode: 'rb')) if File.exist?(cache_file) # rubocop:disable Security/MarshalLoad
  end

  def generate_pin_caches
    Collection.mem_cache[cache_file] = pins
    serial = Marshal.dump(pins)
    FileUtils.mkdir_p File.dirname(cache_file)
    File.write cache_file, serial, mode: 'wb'
    pins
  end
end