Class: Mdlint::CacheStore

Inherits:
Object
  • Object
show all
Defined in:
lib/mdlint/cache_store.rb,
sig/internal.rbs

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ CacheStore

Returns a new instance of CacheStore.

Parameters:

  • path (Object)


9
10
11
12
13
14
# File 'lib/mdlint/cache_store.rb', line 9

def initialize(path)
  @path = path
  @mutex = Mutex.new
  @data = load_data
  @dirty = false
end

Instance Method Details

#canonicalize(value) ⇒ Object

Parameters:

  • value (Object)

Returns:

  • (Object)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mdlint/cache_store.rb', line 65

def canonicalize(value)
  case value
  when Hash
    value.keys.map(&:to_s).sort.to_h do |key|
      original_key = value.keys.find { |candidate| candidate.to_s == key }
      [key, canonicalize(value[original_key])]
    end
  when Array
    value.map { |item| canonicalize(item) }
  when Symbol
    value.to_s
  else
    value
  end
end

#fetch(key) ⇒ Object

Parameters:

  • key (Object)

Returns:

  • (Object)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mdlint/cache_store.rb', line 20

def fetch(key)
  @mutex.synchronize do
    values = @data[key]
    values&.map do |value|
      attributes = symbolize(value)
      Linter::Violation.new(
        rule_id: attributes[:rule_id].to_s,
        message: attributes[:message].to_s,
        line: attributes[:line].to_i,
        column: attributes[:column],
        severity: (attributes[:severity] || :warning).to_sym,
        fixable: attributes[:fixable] == true
      )
    end
  end
end

#key(source, options) ⇒ String

Parameters:

  • source (Object)
  • options (Object)

Returns:

  • (String)


16
17
18
# File 'lib/mdlint/cache_store.rb', line 16

def key(source, options)
  Digest::SHA256.hexdigest(JSON.generate([source, canonicalize(options)]))
end

#load_dataObject

Returns:

  • (Object)


57
58
59
60
61
62
63
# File 'lib/mdlint/cache_store.rb', line 57

def load_data
  return {} unless File.file?(@path)

  JSON.parse(File.read(@path))
rescue JSON::ParserError, SystemCallError
  {}
end

#savevoid

This method returns an undefined value.



44
45
46
47
48
49
50
51
52
53
# File 'lib/mdlint/cache_store.rb', line 44

def save
  @mutex.synchronize do
    return unless @dirty

    parent = File.dirname(@path)
    FileUtils.mkdir_p(parent) unless parent == "."
    File.write(@path, JSON.pretty_generate(@data) + "\n")
    @dirty = false
  end
end

#store(key, violations) ⇒ void

This method returns an undefined value.

Parameters:

  • key (Object)
  • violations (Object)


37
38
39
40
41
42
# File 'lib/mdlint/cache_store.rb', line 37

def store(key, violations)
  @mutex.synchronize do
    @data[key] = violations.map(&:to_h)
    @dirty = true
  end
end

#symbolize(value) ⇒ Object

Parameters:

  • value (Object)

Returns:

  • (Object)


81
82
83
# File 'lib/mdlint/cache_store.rb', line 81

def symbolize(value)
  value.each_with_object({}) { |(key, item), result| result[key.to_sym] = item }
end