Class: Cycromatic::JSONFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/cycromatic/json_formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io:) ⇒ JSONFormatter

Returns a new instance of JSONFormatter.



5
6
7
8
# File 'lib/cycromatic/json_formatter.rb', line 5

def initialize(io:)
  @io = io
  @results = {}
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



3
4
5
# File 'lib/cycromatic/json_formatter.rb', line 3

def io
  @io
end

Instance Method Details

#calculated(path:, complexity:) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cycromatic/json_formatter.rb', line 32

def calculated(path:, complexity:)
  loc = complexity.node.loc
  name = case complexity.type
         when :toplevel
           "[toplevel]"
         when :method
           complexity.node.children.find {|c| c.is_a? Symbol }.to_s
         end

  @path_results << {
    method: name,
    line: [loc.first_line, loc.last_line],
    complexity: complexity.value
  }
end

#completedObject



19
20
21
# File 'lib/cycromatic/json_formatter.rb', line 19

def completed
  @io.puts @results.to_json
end

#error(path:, exception:) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/cycromatic/json_formatter.rb', line 23

def error(path:, exception:)
  @results[path.to_s] = {
    error: {
      message: exception.to_s,
      trace: exception.backtrace
    }
  }
end

#finished(path:) ⇒ Object



15
16
17
# File 'lib/cycromatic/json_formatter.rb', line 15

def finished(path:)
  @path_results = nil
end

#started(path:) ⇒ Object



10
11
12
13
# File 'lib/cycromatic/json_formatter.rb', line 10

def started(path:)
  @path_results = []
  @results[path.to_s] = { results: @path_results }
end