Class: MetricFu::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/metric_fu/loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lib_root) ⇒ Loader

Returns a new instance of Loader.



7
8
9
10
# File 'lib/metric_fu/loader.rb', line 7

def initialize(lib_root)
  @lib_root = lib_root
  @loaded_files = []
end

Instance Attribute Details

#loaded_filesObject (readonly)

TODO: This class mostly serves to clean up the base MetricFu module,

but needs further work


6
7
8
# File 'lib/metric_fu/loader.rb', line 6

def loaded_files
  @loaded_files
end

Instance Method Details

#create_artifact_subdirs(klass) ⇒ Object

Adds method x_dir relative to the metric_fu artifact directory to the given klass

And strips any leading non-alphanumerical character from the directory name

Examples:

For the artifact sub-directory ‘_scratch’, creates on the klass one method:

::scratch_dir (which notably has the leading underscore removed)

Parameters:

  • klass (Class)

    the klass to add methods for the specified artifact sub-directories

Yield Returns:

  • (Array<String>)

    Takes a list of directories and adds readers for each



52
53
54
55
56
57
58
59
60
# File 'lib/metric_fu/loader.rb', line 52

def create_artifact_subdirs(klass)
  class << klass
    Array(yield).each do |dir|
      define_method("#{dir.gsub(/[^A-Za-z0-9]/, '')}_dir") do
        File.join(artifact_dir, dir)
      end
    end
  end
end

#create_dirs(klass) ⇒ Object

TODO: Reduce duplication of directory logic Adds methods x_dir and _x_require for the directory x,

relative to the metric_fu lib, to the given klass

Examples:

For the directory ‘metrics’, which is relative to the metric_fu lib directory,

creates on the klass two methods:
  ::metrics_dir which returns the full path
  ::metrics_require which takes a block of files to require relative to the metrics_dir

Parameters:

  • klass (Class)

    the klass to add methods for the specified directories

Yield Returns:

  • (Array<String>)

    Takes a list of directories and adds readers for each



35
36
37
38
39
40
41
42
43
44
# File 'lib/metric_fu/loader.rb', line 35

def create_dirs(klass)
  class << klass
    Array(yield).each do |dir|
      define_method("#{dir}_dir") do
        File.join(lib_dir, dir)
      end
      module_eval(%(def #{dir}_require(&block); lib_require('#{dir}', &block); end),  __FILE__, __LINE__)
    end
  end
end

#lib_require(base = "", &_block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/metric_fu/loader.rb', line 12

def lib_require(base = "", &_block)
  paths = []
  base_path = File.join(@lib_root, base)
  Array((yield paths, base_path)).each do |path|
    file = File.join(base_path, *Array(path))
    require file
    if @loaded_files.include?(file)
      puts "!!!\tAlready loaded #{file}" if !!(ENV["MF_DEBUG"] =~ /true/i)
    else
      @loaded_files << file
    end
  end
end

#load_installed_metricsObject



81
82
83
84
85
86
# File 'lib/metric_fu/loader.rb', line 81

def load_installed_metrics
  MetricFu.lib_require { "metric" }
  Dir.glob(File.join(MetricFu.metrics_dir, "**/metric.rb")).each do |metric_config|
    require metric_config
  end
end

#load_metric_configurationObject



74
75
76
77
78
79
# File 'lib/metric_fu/loader.rb', line 74

def load_metric_configuration
  MetricFu.lib_require { "configuration" }
  load_installed_metrics
  MetricFu.configuration.configure_metrics
  load_user_configuration
end

#load_tasks(tasks_relative_path, options = { task_name: "" }) ⇒ Object

Load specified task task only once

if and only if rake is required and the task is not yet defined
to prevent the task from being loaded multiple times

Parameters:

  • tasks_relative_path (String)

    ‘metric_fu.rake’ by default

  • options (Hash) (defaults to: { task_name: "" })

    optional task_name to check if loaded

Options Hash (options):

  • :task_name (String)

    The task_name to load, if not yet loaded



99
100
101
102
103
# File 'lib/metric_fu/loader.rb', line 99

def load_tasks(tasks_relative_path, options = { task_name: "" })
  if defined?(Rake::Task) and not Rake::Task.task_defined?(options[:task_name])
    load File.join(@lib_root, "tasks", *Array(tasks_relative_path))
  end
end

#load_user_configurationObject



88
89
90
91
# File 'lib/metric_fu/loader.rb', line 88

def load_user_configuration
  file = File.join(MetricFu.run_dir, ".metrics")
  load file if File.exist?(file)
end

#setupObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/metric_fu/loader.rb', line 62

def setup
  MetricFu.lib_require { "logger" }
  MetricFu.logger.debug_on = !!(ENV["MF_DEBUG"] =~ /true/i)

  load_metric_configuration

  MetricFu.lib_require       { "reporter" }
  MetricFu.reporting_require { "result" }

  MetricFu.load_tasks("metric_fu.rake", task_name: "metrics:all")
end