Class: ArchSpec::Todo

Inherits:
Object
  • Object
show all
Defined in:
lib/archspec/todo.rb

Overview

A file of accepted existing violations. ArchSpec still checks the files these come from, but subtracts the recorded violations so you can adopt it in an existing app and burn the list down over time. Matched by ArchSpec::Diagnostic#fingerprint, so entries survive edits that shift lines.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ids, root:) ⇒ Todo

Returns a new instance of Todo.



42
43
44
45
# File 'lib/archspec/todo.rb', line 42

def initialize(ids, root:)
  @ids = ids
  @root = root
end

Class Method Details

.empty(root: nil) ⇒ Object



11
12
13
# File 'lib/archspec/todo.rb', line 11

def self.empty(root: nil)
  new(Set.new, root: root)
end

.load(path, root:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/archspec/todo.rb', line 15

def self.load(path, root:)
  return empty(root: root) unless path && File.exist?(path)

  document = YAML.safe_load_file(path, permitted_classes: [], aliases: false) || {}
  ids = Array(document['violations']).filter_map do |entry|
    entry.is_a?(Hash) ? entry['id'] : entry
  end

  new(ids.to_set, root: root)
end

.write(path, diagnostics, root:) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/archspec/todo.rb', line 26

def self.write(path, diagnostics, root:)
  payload = {
    'violations' => diagnostics.map do |diagnostic|
      {
        'id' => diagnostic.fingerprint(root: root),
        'rule' => diagnostic.rule,
        'path' => diagnostic.location.relative_path(root),
        'line' => diagnostic.location.line,
        'message' => diagnostic.message
      }
    end
  }

  File.write(path, payload.to_yaml)
end

Instance Method Details

#include?(diagnostic) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/archspec/todo.rb', line 47

def include?(diagnostic)
  ids.include?(diagnostic.fingerprint(root: root))
end