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.



57
58
59
60
# File 'lib/archspec/todo.rb', line 57

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
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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) || {}
  unless document.is_a?(Hash) && (document['violations'].nil? || document['violations'].is_a?(Array))
    raise Error, "invalid todo file #{path}: expected a violations list"
  end

  ids = Array(document['violations']).map.with_index do |entry, index|
    id = entry.is_a?(Hash) ? entry['id'] : entry
    unless id.is_a?(String) && !id.empty?
      raise Error, "invalid todo file #{path}: violation #{index + 1} has no id"
    end

    id
  end

  new(ids.to_set, root: root)
rescue Error
  raise
rescue Psych::Exception, SystemCallError => e
  raise Error, "could not load todo file #{path}: #{e.message}"
end

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/archspec/todo.rb', line 39

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)
rescue SystemCallError => e
  raise Error, "could not write todo file #{path}: #{e.message}"
end

Instance Method Details

#include?(diagnostic) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/archspec/todo.rb', line 62

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