Class: MetricFu::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/base/generator.rb

Overview

Generator

The Generator class is an abstract class that provides the skeleton for producing different types of metrics.

It drives the production of the metrics through a template method - #generate_report(options={}). This method calls #emit, #analyze and #to_h in order to produce the metrics.

To implement a concrete class to generate a metric, therefore, the class must implement those three methods.

  • #emit should take care of running the metric tool and gathering its output.

  • #analyze should take care of manipulating the output from #emit and making it possible to store it in a programmatic way.

  • #to_h should provide a hash representation of the output from #analyze ready to be serialized into yaml at some point.

Pre-conditions

Based on the class name of the concrete class implementing a Generator, the Generator class will create a ‘metric_directory’ named after the class under the MetricFu.scratch_directory, where any output from the #emit method should go.

It will also create the MetricFu.output_directory if neccessary, and in general setup the directory structure that the MetricFu system expects.

Direct Known Subclasses

Churn, Flay, Flog, Flog::Page, Rcov, Reek, Roodi, Saikuro, Stats

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Generator

Returns a new instance of Generator.



35
36
37
38
39
40
# File 'lib/base/generator.rb', line 35

def initialize(options={})
  self.class.verify_dependencies!
  create_metric_dir_if_missing
  create_output_dir_if_missing
  create_data_dir_if_missing
end

Instance Attribute Details

#reportObject (readonly)

Returns the value of attribute report.



33
34
35
# File 'lib/base/generator.rb', line 33

def report
  @report
end

#templateObject (readonly)

Returns the value of attribute template.



33
34
35
# File 'lib/base/generator.rb', line 33

def template
  @template
end

Class Method Details

.class_nameObject

Provides the unqualified class name of an implemented concrete class, as a string. For example:

class Flay < Generator; end
klass = Flay.new
klass.class_name
> "flay"

Returns:

  • String The unqualified class name of this concrete class, returned as a string.



67
68
69
# File 'lib/base/generator.rb', line 67

def self.class_name
  self.to_s.split('::').last.downcase
end

.generate_report(options = {}) ⇒ Object

Creates a new generator and returns the output of the #generate_report method. This is the typical way to generate a new MetricFu report. For more information see the #generate_report instance method.

See Also:



51
52
53
54
# File 'lib/base/generator.rb', line 51

def self.generate_report(options={})
  generator = self.new(options)
  generator.generate_report
end

.metric_directoryObject

Returns the directory where the Generator will write any output



72
73
74
# File 'lib/base/generator.rb', line 72

def self.metric_directory
  File.join(MetricFu.scratch_directory, class_name) 
end

.verify_dependencies!Object

Allows subclasses to check for required gems



132
133
134
# File 'lib/base/generator.rb', line 132

def self.verify_dependencies!
  true
end

Instance Method Details

#analyzeObject

:nodoc:



144
145
146
147
148
149
150
# File 'lib/base/generator.rb', line 144

def analyze #:nodoc:
  raise <<-EOF
    This method must be implemented by a concrete class descending
    from Generator.  See generator class documentation for more 
    information.
  EOF
end

#create_data_dir_if_missingObject

:nodoc:



88
89
90
91
92
# File 'lib/base/generator.rb', line 88

def create_data_dir_if_missing #:nodoc:
  unless File.directory?(MetricFu.data_directory)
    FileUtils.mkdir_p(MetricFu.data_directory, :verbose => false) 
  end
end

#create_metric_dir_if_missingObject

:nodoc:



76
77
78
79
80
# File 'lib/base/generator.rb', line 76

def create_metric_dir_if_missing #:nodoc:
  unless File.directory?(metric_directory)
    FileUtils.mkdir_p(metric_directory, :verbose => false) 
  end
end

#create_output_dir_if_missingObject

:nodoc:



82
83
84
85
86
# File 'lib/base/generator.rb', line 82

def create_output_dir_if_missing #:nodoc:
  unless File.directory?(MetricFu.output_directory)
    FileUtils.mkdir_p(MetricFu.output_directory, :verbose => false) 
  end
end

#emitObject

:nodoc:



136
137
138
139
140
141
142
# File 'lib/base/generator.rb', line 136

def emit #:nodoc:
  raise <<-EOF
    This method must be implemented by a concrete class descending
    from Generator.  See generator class documentation for more 
    information.
  EOF
end

#generate_reportObject

Provides a template method to drive the production of a metric from a concrete implementation of this class. Each concrete class must implement the three methods that this template method calls: #emit, #analyze and #to_h. For more details, see the class documentation.

This template method also calls before_emit, after_emit… etc. methods to allow extra hooks into the processing methods, and help to keep the logic of your Generators clean.



117
118
119
120
121
122
123
124
125
# File 'lib/base/generator.rb', line 117

def generate_report
  %w[emit analyze].each do |meth|
    send("before_#{meth}".to_sym)
    send("#{meth}".to_sym)
    send("after_#{meth}".to_sym)
  end
  before_to_h()
  to_h()
end

#metric_directoryObject

Returns String The path of the metric directory this class is using.

Returns:

  • String The path of the metric directory this class is using.



96
97
98
# File 'lib/base/generator.rb', line 96

def metric_directory
  self.class.metric_directory
end

#round_to_tenths(decimal) ⇒ Object



127
128
129
# File 'lib/base/generator.rb', line 127

def round_to_tenths(decimal)
  (decimal * 10).round / 10.0 
end

#to_graphObject

:nodoc:



152
153
154
155
156
157
158
# File 'lib/base/generator.rb', line 152

def to_graph #:nodoc:
  raise <<-EOF
    This method must be implemented by a concrete class descending
    from Generator.  See generator class documentation for more 
    information.
  EOF
end