class MetricFu::Generator

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_result(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.

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 metric under the scratch_directory, where any output from the emit method should go.

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

Attributes

options[R]
result[R]
template[R]

Public Class Methods

generators() click to toggle source

@return all subclassed generators [Array<MetricFu::Generator>]

# File lib/metric_fu/generator.rb, line 49
def self.generators
  @generators
end
get_generator(metric) click to toggle source
# File lib/metric_fu/generator.rb, line 53
def self.get_generator(metric)
  generators.find { |generator|generator.metric.to_s == metric.to_s.downcase }
end
inherited(subclass) click to toggle source
# File lib/metric_fu/generator.rb, line 57
def self.inherited(subclass)
  @generators << subclass
end
metric() click to toggle source
# File lib/metric_fu/generator.rb, line 39
def self.metric
  not_implemented
end
metric_directory() click to toggle source

Returns the directory where the Generator will write any output

# File lib/metric_fu/generator.rb, line 62
def self.metric_directory
  @metric_directory ||=
    MetricFu::Metric.get_metric(metric).run_options[:output_directory] ||
    begin
      metric_directory = MetricFu::Io::FileSystem.scratch_directory(metric)
      MetricFu::Utility.mkdir_p(metric_directory, verbose: false)
    end
end
new(options = {}) click to toggle source
# File lib/metric_fu/generator.rb, line 35
def initialize(options = {})
  @options = options
end
not_implemented() click to toggle source
# File lib/metric_fu/generator.rb, line 141
    def self.not_implemented
      raise NotImplementedError.new <<-EOF
        Required method #{caller[0]} not implemented in #{__FILE__}.
        This method must be implemented by a concrete class descending
        from Generator.  See generator class documentation for more
        information.
      EOF
    end

Public Instance Methods

generate_result() click to toggle source

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.

# File lib/metric_fu/generator.rb, line 117
def generate_result
  mf_debug "Executing #{metric}"
  emit
  analyze
  to_h
end
metric() click to toggle source
# File lib/metric_fu/generator.rb, line 43
def metric
  self.class.metric
end
metric_config() click to toggle source
# File lib/metric_fu/generator.rb, line 85
def metric_config
  MetricFu::Metric.get_metric(metric)
end
metric_directory() click to toggle source

@return [String] The path of the metric directory this class is using.

# File lib/metric_fu/generator.rb, line 73
def metric_directory
  self.class.metric_directory
end
remove_excluded_files(paths, globs_to_remove = MetricFu::Io::FileSystem.file_globs_to_ignore) click to toggle source
# File lib/metric_fu/generator.rb, line 77
def remove_excluded_files(paths, globs_to_remove = MetricFu::Io::FileSystem.file_globs_to_ignore)
  files_to_remove = []
  globs_to_remove.each do |glob|
    files_to_remove.concat(Dir[glob])
  end
  paths - files_to_remove
end
round_to_tenths(decimal) click to toggle source
# File lib/metric_fu/generator.rb, line 124
def round_to_tenths(decimal)
  decimal = 0.0 if decimal.to_s.eql?("NaN")
  (decimal * 10).round / 10.0
end
run!(args) click to toggle source
# File lib/metric_fu/generator.rb, line 89
def run!(args)
  metric_config.run_external(args)
end
silence_streams(*streams) { || ... } click to toggle source

temporarily redirect stderr usage: silence_streams(STDERR) { STDERR.puts “something wrong” }

# File lib/metric_fu/generator.rb, line 95
def silence_streams(*streams)
  on_hold = streams.collect { |stream| stream.dup }
  streams.each do |stream|
    stream.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null')
    stream.sync = true
  end
  yield
ensure
  streams.each_with_index do |stream, i|
    stream.reopen(on_hold[i])
  end
end