Class: Hashira::Coupling::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/hashira/coupling/graph.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, trees, census) ⇒ Graph

Returns a new instance of Graph.



4
5
6
7
8
9
10
# File 'lib/hashira/coupling/graph.rb', line 4

def initialize(project, trees, census)
  @census = census
  @trees = trees
  @map = Hashira::Coupling::EdgeMap.new(census)
  trees.each { |file, tree| @map.record(project.relative(file), file, tree) }
  @cycles = Hashira::Coupling::Cycles.new(@map.dependencies, self)
end

Instance Attribute Details

#cyclesObject (readonly)

Returns the value of attribute cycles.



12
13
14
# File 'lib/hashira/coupling/graph.rb', line 12

def cycles
  @cycles
end

#treesObject (readonly)

Returns the value of attribute trees.



12
13
14
# File 'lib/hashira/coupling/graph.rb', line 12

def trees
  @trees
end

Instance Method Details

#charge(file) ⇒ Object



14
# File 'lib/hashira/coupling/graph.rb', line 14

def charge(file) = @census.charge(file, [])

#constants(edge) ⇒ Object



41
42
43
44
# File 'lib/hashira/coupling/graph.rb', line 41

def constants(edge)
  from, to = edge.deconstruct
  @map.usage[[from, to]].sort
end

#edgesObject



26
27
28
# File 'lib/hashira/coupling/graph.rb', line 26

def edges
  dependencies.sort.flat_map { |from, tos| tos.sort.map { Hashira::Coupling::Edge.new(from:, to: it) } }
end

#evidence(from, to) ⇒ Object



37
# File 'lib/hashira/coupling/graph.rb', line 37

def evidence(from, to) = @map.evidence[[from, to]]

#foldsObject



20
# File 'lib/hashira/coupling/graph.rb', line 20

def folds = @census.folds

#incoming(package) ⇒ Object



24
# File 'lib/hashira/coupling/graph.rb', line 24

def incoming(package) = packages.select { dependencies[it].include?(package) }.sort

#metric(package) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/hashira/coupling/graph.rb', line 46

def metric(package)
  Hashira::Coupling::Metric.new(
    types: @census.types[package],
    afferent: incoming(package).size,
    efferent: dependencies[package].size
  )
end

#metricsObject



54
# File 'lib/hashira/coupling/graph.rb', line 54

def metrics = packages.to_h { [it, metric(it)] }

#outgoing(package) ⇒ Object



22
# File 'lib/hashira/coupling/graph.rb', line 22

def outgoing(package) = dependencies[package].to_a.sort

#packagesObject



16
# File 'lib/hashira/coupling/graph.rb', line 16

def packages = (@census.packages | @map.dependencies.keys)

#packagingObject



18
# File 'lib/hashira/coupling/graph.rb', line 18

def packaging = @census.packaging

#usage(package) ⇒ Object



39
# File 'lib/hashira/coupling/graph.rb', line 39

def usage(package) = incoming(package).to_h { [it, @map.usage[[it, package]]] }

#violationsObject



56
# File 'lib/hashira/coupling/graph.rb', line 56

def violations = Hashira::Coupling::SdpCheck.new(dependencies, metrics).violations

#weight(from, to) ⇒ Object



58
# File 'lib/hashira/coupling/graph.rb', line 58

def weight(from, to) = evidence(from, to).size

#weightedObject



30
31
32
33
34
35
# File 'lib/hashira/coupling/graph.rb', line 30

def weighted
  edges.map do |edge|
    from, to = edge.deconstruct
    [from, to, weight(from, to)]
  end
end