Class: MetricFu::Configuration

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

Overview

Configuration

The Configuration class, as it sounds, provides methods for configuring the behaviour of MetricFu.

Customization for Rails

The Configuration class checks for the presence of a ‘config/environment.rb’ file. If the file is present, it assumes it is running in a Rails project. If it is, it will:

  • Add ‘app’ to the @code_dirs directory to include the code in the app directory in the processing

  • Add :stats to the list of metrics to run to get the Rails stats task

Customization for CruiseControl.rb

The Configuration class checks for the presence of a ‘CC_BUILD_ARTIFACTS’ environment variable. If it’s found it will change the default output directory from the default “tmp/metric_fu to the directory represented by ‘CC_BUILD_ARTIFACTS’

Deprications

The Configuration class checks for several deprecated constants that were previously used to configure MetricFu. These include CHURN_OPTIONS, DIRECTORIES_TO_FLOG, SAIKURO_OPTIONS, and MetricFu::SAIKURO_OPTIONS.

These have been replaced by config.churn, config.flog and config.saikuro respectively.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

:nodoc:#



53
54
55
56
57
58
# File 'lib/base/configuration.rb', line 53

def initialize #:nodoc:#
  warn_about_deprecated_config_options
  reset
  add_attr_accessors_to_self
  add_class_methods_to_metric_fu
end

Class Method Details

.run {|MetricFu.configuration| ... } ⇒ Object

This allows us to have a nice syntax like:

MetricFu.run do |config|
  config.base_directory = 'tmp/metric_fu'
end

See the README for more information on configuration options.



111
112
113
# File 'lib/base/configuration.rb', line 111

def self.run
  yield MetricFu.configuration
end

Instance Method Details

#add_attr_accessors_to_selfObject

Searches through the instance variables of the class and creates an attribute accessor on this instance of the Configuration class for each instance variable.



78
79
80
81
82
83
# File 'lib/base/configuration.rb', line 78

def add_attr_accessors_to_self
  instance_variables.each do |name|
    method_name = name[1..-1].to_sym
    MetricFu::Configuration.send(:attr_accessor, method_name)
  end
end

#add_class_methods_to_metric_fuObject

Searches through the instance variables of the class and creates a class method on the MetricFu module to read the value of the instance variable from the Configuration class.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/base/configuration.rb', line 63

def add_class_methods_to_metric_fu
  instance_variables.each do |name|
    method_name = name[1..-1].to_sym
    method = <<-EOF
              def self.#{method_name}
                configuration.send(:#{method_name})
              end
    EOF
    MetricFu.module_eval(method)
  end
end

#is_cruise_control_rb?Boolean

Returns:

  • (Boolean)


203
204
205
# File 'lib/base/configuration.rb', line 203

def is_cruise_control_rb?
  !!ENV['CC_BUILD_ARTIFACTS']
end

#platformObject

:nodoc:



199
200
201
# File 'lib/base/configuration.rb', line 199

def platform #:nodoc:
  return PLATFORM
end

#rails?Boolean

TODO:

This should probably be made a bit more robust.

Perform a simple check to try and guess if we’re running against a rails app.

Returns:

  • (Boolean)


172
173
174
# File 'lib/base/configuration.rb', line 172

def rails?
  @rails = File.exist?("config/environment.rb")
end

#resetObject

This does the real work of the Configuration class, by setting up a bunch of instance variables to represent the configuration of the MetricFu app.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/base/configuration.rb', line 118

def reset
  @base_directory = ENV['CC_BUILD_ARTIFACTS'] || 'tmp/metric_fu'
  @scratch_directory = File.join(@base_directory, 'scratch')
  @output_directory = File.join(@base_directory, 'output')
  @data_directory = File.join('tmp/metric_fu', '_data')
  @metric_fu_root_directory = File.join(File.dirname(__FILE__), 
                                                    '..', '..')
  @template_directory =  File.join(@metric_fu_root_directory, 
                                   'lib', 'templates') 
  @template_class = AwesomeTemplate
  set_metrics
  set_graphs
  set_code_dirs
  @flay     = { :dirs_to_flay => @code_dirs  } 
  @flog     = { :dirs_to_flog => @code_dirs  }
  @reek     = { :dirs_to_reek => @code_dirs  }
  @roodi    = { :dirs_to_roodi => @code_dirs }
  @saikuro  = { :output_directory => @scratch_directory + '/saikuro', 
                :input_directory => @code_dirs,
                :cyclo => "",
                :filter_cyclo => "0",
                :warn_cyclo => "5",
                :error_cyclo => "7",
                :formater => "text"}
  @churn    = {}
  @stats    = {}
  @rcov     = { :test_files => ['test/**/*_test.rb', 
                                'spec/**/*_spec.rb'],
                :rcov_opts => ["--sort coverage", 
                               "--no-html", 
                               "--text-coverage",
                               "--no-color",
                               "--profile",
                               "--rails",
                               "--exclude /gems/,/Library/,/usr/,spec"]}
                               
  @graph_theme = { :colors => %w(orange purple green white red blue pink yellow),
                   :marker_color => 'blue',
                   :background_colors => %w(white white)}
  
  relative_font_path = [File.dirname(__FILE__), '..', '..', 'vendor', '_fonts', 'monaco.ttf']
  @graph_font = File.expand_path(File.join(relative_font_path))
  @graph_size = "1000x400"
  @graph_title_font_size = 12
  @graph_legend_box_size = 12
  @graph_legend_font_size = 10
  @graph_marker_font_size = 10
  
end

#set_code_dirsObject

Add the ‘app’ directory if we’re running within rails.



191
192
193
194
195
196
197
# File 'lib/base/configuration.rb', line 191

def set_code_dirs
  if rails?
    @code_dirs = ['app', 'lib']
  else
    @code_dirs = ['lib']
  end
end

#set_graphsObject



186
187
188
# File 'lib/base/configuration.rb', line 186

def set_graphs
  @graphs = MetricFu::AVAILABLE_GRAPHS 
end

#set_metricsObject

Add the :stats task to the AVAILABLE_METRICS constant if we’re running within rails.



178
179
180
181
182
183
184
# File 'lib/base/configuration.rb', line 178

def set_metrics
  if rails?
    @metrics = MetricFu::AVAILABLE_METRICS + [:stats]
  else
    @metrics = MetricFu::AVAILABLE_METRICS
  end 
end

#warn_about_deprecated_config_optionsObject

Check if certain constants that are deprecated have been assigned. If so, warn the user about them, and the fact that they will have no effect.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/base/configuration.rb', line 88

def warn_about_deprecated_config_options
  if defined?(::MetricFu::CHURN_OPTIONS)
    raise("Use config.churn instead of MetricFu::CHURN_OPTIONS")
  end
  if defined?(::MetricFu::DIRECTORIES_TO_FLOG)
    raise("Use config.flog[:dirs_to_flog] "+
          "instead of MetricFu::DIRECTORIES_TO_FLOG") 
  end
  if defined?(::MetricFu::SAIKURO_OPTIONS)
    raise("Use config.saikuro instead of MetricFu::SAIKURO_OPTIONS")
  end
  if defined?(SAIKURO_OPTIONS)
    raise("Use config.saikuro instead of SAIKURO_OPTIONS")
  end
end