Class: Fukuzatsu::Summary

Inherits:
Object
  • Object
show all
Defined in:
lib/fukuzatsu/summary.rb

Constant Summary collapse

BRANCHES =
[
  :if,
  :or_asgn,
  :and_sgn,
  :or,
  :and
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source:, entity:, container:, source_file: nil, summaries: []) ⇒ Summary

Returns a new instance of Summary.



42
43
44
45
46
47
48
49
# File 'lib/fukuzatsu/summary.rb', line 42

def initialize(source:, entity:, container:, source_file:nil, summaries:[])
  @source = source
  @entity = entity
  @container = container
  @source_file = source_file
  @summaries = summaries
  @edges, @nodes, @exits = [0, 1, 1]
end

Instance Attribute Details

#containerObject (readonly)

Returns the value of attribute container.



5
6
7
# File 'lib/fukuzatsu/summary.rb', line 5

def container
  @container
end

#edgesObject

Returns the value of attribute edges.



6
7
8
# File 'lib/fukuzatsu/summary.rb', line 6

def edges
  @edges
end

#entityObject (readonly)

Returns the value of attribute entity.



5
6
7
# File 'lib/fukuzatsu/summary.rb', line 5

def entity
  @entity
end

#exitsObject

Returns the value of attribute exits.



6
7
8
# File 'lib/fukuzatsu/summary.rb', line 6

def exits
  @exits
end

#nodesObject

Returns the value of attribute nodes.



6
7
8
# File 'lib/fukuzatsu/summary.rb', line 6

def nodes
  @nodes
end

#source_fileObject (readonly)

Returns the value of attribute source_file.



5
6
7
# File 'lib/fukuzatsu/summary.rb', line 5

def source_file
  @source_file
end

#summariesObject

Returns the value of attribute summaries.



6
7
8
# File 'lib/fukuzatsu/summary.rb', line 6

def summaries
  @summaries
end

Class Method Details

.from(content:, source_file: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fukuzatsu/summary.rb', line 16

def self.from(content:, source_file:nil)
  parser = Analyst.for_source(content)

  containers = parser.top_level_entities.select{|e| e.respond_to? :all_methods}
  containers << containers.map(&:classes)
  containers.flatten!.reject!{ |container| container.all_methods.empty? }

  containers.map do |container|
    summary = Fukuzatsu::Summary.new(
      container: container,
      source: container.send(:source),
      entity: container,
      source_file: source_file
    )
    summary.summaries = container.all_methods.map do |method|
      Fukuzatsu::Summary.new(
        container: container,
        source: method.send(:source),
        entity: method,
        source_file: source_file
      )
    end
    summary
  end
end

Instance Method Details

#average_complexityObject



71
72
73
74
# File 'lib/fukuzatsu/summary.rb', line 71

def average_complexity
  return 0 if self.summaries.blank?
  self.summaries.map(&:complexity).reduce(&:+) / self.summaries.count.to_f
end

#complexityObject



51
52
53
54
55
56
# File 'lib/fukuzatsu/summary.rb', line 51

def complexity
  @complexity ||= begin
    traverse(self.entity.ast)
    self.edges - self.nodes + self.exits
  end
end

#container_nameObject



58
59
60
# File 'lib/fukuzatsu/summary.rb', line 58

def container_name
  self.container.full_name
end

#entity_nameObject



66
67
68
69
# File 'lib/fukuzatsu/summary.rb', line 66

def entity_name
  return "*" if is_class_or_module?
  self.entity.full_name.gsub(self.container.full_name, '')
end

#is_class_or_module?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/fukuzatsu/summary.rb', line 62

def is_class_or_module?
  self.container == self.entity
end

#raw_sourceObject



76
77
78
# File 'lib/fukuzatsu/summary.rb', line 76

def raw_source
  self.source_file && "\n#{File.open(self.source_file, "r").read}" || @source
end