Class: Turbulence::Generators::TreeMap

Inherits:
Object
  • Object
show all
Defined in:
lib/turbulence/generators/treemap.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metrics_hash, x_metric = :churn, y_metric = :complexity) ⇒ TreeMap

Returns a new instance of TreeMap.



6
7
8
9
10
11
12
# File 'lib/turbulence/generators/treemap.rb', line 6

def initialize(metrics_hash,
               x_metric = :churn,
               y_metric = :complexity)
  @x_metric     = x_metric
  @y_metric     = y_metric
  @metrics_hash = metrics_hash
end

Instance Attribute Details

#metrics_hashObject (readonly)

Returns the value of attribute metrics_hash.



4
5
6
# File 'lib/turbulence/generators/treemap.rb', line 4

def metrics_hash
  @metrics_hash
end

#x_metricObject (readonly)

Returns the value of attribute x_metric.



4
5
6
# File 'lib/turbulence/generators/treemap.rb', line 4

def x_metric
  @x_metric
end

#y_metricObject (readonly)

Returns the value of attribute y_metric.



4
5
6
# File 'lib/turbulence/generators/treemap.rb', line 4

def y_metric
  @y_metric
end

Class Method Details

.from(metrics_hash) ⇒ Object



47
48
49
# File 'lib/turbulence/generators/treemap.rb', line 47

def self.from(metrics_hash)
  new(metrics_hash)
end

Instance Method Details

#build_jsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/turbulence/generators/treemap.rb', line 21

def build_js
  clean_metrics_from_missing_data

  # Build Highcharts treemap data format
  data = @metrics_hash.map do |filename, metrics|
    churn = metrics[@x_metric] || 0
    complexity = metrics[@y_metric] || 0
    # Ensure minimum value of 1 for churn to avoid zero-size boxes
    churn = 1 if churn.zero?
    {
      name: filename,
      value: churn,
      colorValue: complexity
    }
  end

  "var treemap_data = #{data.to_json};"
end

#generate_results(metrics, cli) ⇒ Object



14
15
16
17
18
19
# File 'lib/turbulence/generators/treemap.rb', line 14

def generate_results(metrics, cli)
  File.open("treemap_data.js", "w") do |f|
    cli.copy_templates_into(Dir.pwd)
    f.write Turbulence::Generators::TreeMap.from(metrics).build_js
  end
end