class MetricFu::Metric

Attributes

activated[RW]
enabled[RW]

Public Class Methods

enabled_metrics() click to toggle source
# File lib/metric_fu/metric.rb, line 91
def self.enabled_metrics
  metrics.select { |metric| metric.enabled && metric.activated }.sort_by { |metric| metric.name  == :hotspots ? 1 : 0 }
end
get_metric(name) click to toggle source
# File lib/metric_fu/metric.rb, line 95
def self.get_metric(name)
  metrics.find { |metric|metric.name.to_s == name.to_s }
end
inherited(subclass) click to toggle source
# File lib/metric_fu/metric.rb, line 99
def self.inherited(subclass)
  @metrics << subclass.new
end
metrics() click to toggle source

@return all subclassed metrics [Array<MetricFu::Metric>] ensure :hotspots runs last

# File lib/metric_fu/metric.rb, line 87
def self.metrics
  @metrics
end
new() click to toggle source
# File lib/metric_fu/metric.rb, line 9
def initialize
  self.enabled = false
  @libraries = Set.new
  @configured_run_options = {}
end

Public Instance Methods

activate() click to toggle source

TODO: Confirm this catches load errors from requires in subclasses, such as for flog

# File lib/metric_fu/metric.rb, line 20
def activate
  MetricFu.metrics_require { default_metric_library_paths }
  @libraries.each { |library| require(library) }
  self.activated = true
rescue LoadError => e
  mf_log "#{name} metric not activated, #{e.message}"
end
configured_run_options() click to toggle source
# File lib/metric_fu/metric.rb, line 70
def configured_run_options
  @configured_run_options
end
default_run_args() click to toggle source
# File lib/metric_fu/metric.rb, line 42
def default_run_args
  run_options.map { |k, v| "--#{k} #{v}" }.join(" ")
end
default_run_options() click to toggle source

@return [Hash] default metric run options

# File lib/metric_fu/metric.rb, line 75
def default_run_options
  not_implemented
end
enable() click to toggle source
# File lib/metric_fu/metric.rb, line 15
def enable
  self.enabled = true
end
gem_name() click to toggle source
# File lib/metric_fu/metric.rb, line 33
def gem_name
  name
end
has_graph?() click to toggle source

@return [Hash] metric_options

# File lib/metric_fu/metric.rb, line 80
def has_graph?
  not_implemented
end
name() click to toggle source

@return metric name [Symbol]

# File lib/metric_fu/metric.rb, line 29
def name
  not_implemented
end
run() click to toggle source
# File lib/metric_fu/metric.rb, line 46
def run
  not_implemented
end
run_external(args = default_run_args) click to toggle source
# File lib/metric_fu/metric.rb, line 50
def run_external(args = default_run_args)
  runner = GemRun.new(
    gem_name: gem_name.to_s,
    metric_name: name.to_s,
    # version: ,
    args: args,
  )
  stdout, stderr, status = runner.run
  # TODO: do something with the stderr
  # for now, just acknowledge we got it
  unless stderr.empty?
    STDERR.puts "STDERR from #{gem_name}:\n#{stderr}"
  end
  # TODO: status.success? is not reliable for distinguishing
  # between a successful run of the metric and problems
  # found by the metric. Talk to other metrics about this.
  MetricFu.logger.debug "#{gem_name} ran with #{status.success? ? 'success' : 'failure'} code #{status.exitstatus}"
  stdout
end
run_options() click to toggle source

@return metric run options [Hash]

# File lib/metric_fu/metric.rb, line 38
def run_options
  default_run_options.merge(configured_run_options)
end

Protected Instance Methods

method_missing(method, *args) click to toggle source

Enable using a syntax such as metric.foo = 'foo'

by catching the missing method here,
checking if :foo is a key in the default_run_options, and
setting the key/value in the @configured_run_options hash

TODO: See if we can do this without a method_missing

# File lib/metric_fu/metric.rb, line 110
def method_missing(method, *args)
  key = method_to_attr(method)
  if default_run_options.has_key?(key)
    configured_run_options[key] = args.first
  else
    raise "#{key} is not a valid configuration option"
  end
end
method_to_attr(method) click to toggle source

Used above to identify the stem of a setter method

# File lib/metric_fu/metric.rb, line 120
def method_to_attr(method)
  method.to_s.sub(/=$/, "").to_sym
end

Private Instance Methods

activate_library(file) click to toggle source
# File lib/metric_fu/metric.rb, line 130
def activate_library(file)
  @libraries << file.strip
end
default_metric_library_paths() click to toggle source
# File lib/metric_fu/metric.rb, line 134
def default_metric_library_paths
  paths = []
  paths << generator_path = "#{name}/generator"
  if has_graph?
    paths << grapher_path   = "#{name}/grapher"
  end
  paths
end
not_implemented() click to toggle source
# File lib/metric_fu/metric.rb, line 126
def not_implemented
  raise "Required method #{caller[0]} not implemented in #{__FILE__}"
end