Class: Mdlint::CacheStore
- Inherits:
-
Object
- Object
- Mdlint::CacheStore
- Defined in:
- lib/mdlint/cache_store.rb,
sig/internal.rbs
Instance Method Summary collapse
- #canonicalize(value) ⇒ Object
- #fetch(key) ⇒ Object
-
#initialize(path) ⇒ CacheStore
constructor
A new instance of CacheStore.
- #key(source, options) ⇒ String
- #load_data ⇒ Object
- #save ⇒ void
- #store(key, violations) ⇒ void
- #symbolize(value) ⇒ Object
Constructor Details
#initialize(path) ⇒ CacheStore
Returns a new instance of CacheStore.
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
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
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
16 17 18 |
# File 'lib/mdlint/cache_store.rb', line 16 def key(source, ) Digest::SHA256.hexdigest(JSON.generate([source, canonicalize()])) end |
#load_data ⇒ 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 |
#save ⇒ void
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.
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
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 |