Class: MetricFu::Loader
- Inherits:
-
Object
- Object
- MetricFu::Loader
- Defined in:
- lib/metric_fu/loader.rb
Instance Attribute Summary collapse
-
#loaded_files ⇒ Object
readonly
TODO: This class mostly serves to clean up the base MetricFu module, but needs further work.
Instance Method Summary collapse
-
#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.
-
#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.
-
#initialize(lib_root) ⇒ Loader
constructor
A new instance of Loader.
- #lib_require(base = "", &_block) ⇒ Object
- #load_installed_metrics ⇒ Object
- #load_metric_configuration ⇒ Object
-
#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.
- #load_user_configuration ⇒ Object
- #setup ⇒ Object
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_files ⇒ Object (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
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
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_metrics ⇒ Object
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_configuration ⇒ Object
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
99 100 101 102 103 |
# File 'lib/metric_fu/loader.rb', line 99 def load_tasks(tasks_relative_path, = { task_name: "" }) if defined?(Rake::Task) and not Rake::Task.task_defined?([:task_name]) load File.join(@lib_root, "tasks", *Array(tasks_relative_path)) end end |
#load_user_configuration ⇒ Object
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 |
#setup ⇒ Object
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 |