class MetricFu::Template

Attributes

output_directory[RW]

Public Instance Methods

render_partial(name, instance_variables = {}) click to toggle source

Renders a partial and add optional instance variables to the template @param name <String> name of partial, omitting leading underscore (_) @param instance_variables <Hash> of instance variable names and values to set

# File lib/metric_fu/templates/template.rb, line 14
def render_partial(name, instance_variables = {})
  create_instance_vars(instance_variables)
  erbify("_#{name}")
end

Private Instance Methods

complete_file_path(filename) click to toggle source
# File lib/metric_fu/templates/template.rb, line 192
def complete_file_path(filename)
  File.expand_path(filename)
end
create_instance_var(section, contents) click to toggle source

Copies an instance variable mimicing the name of the section we are trying to render, with a value equal to the passed in constant. Allows the concrete template classes to refer to that instance variable from their ERB rendering

@param section String

The name of the instance variable to create

@param contents Object

The value to set as the value of the created instance
variable
# File lib/metric_fu/templates/template.rb, line 57
def create_instance_var(section, contents)
  instance_variable_set("@#{section}", contents)
end
create_instance_vars(variables) click to toggle source
# File lib/metric_fu/templates/template.rb, line 61
def create_instance_vars(variables)
  variables.each { |variable| create_instance_var(*variable) }
end
cycle(first_value, second_value, iteration) click to toggle source

Provides a brain dead way to cycle between two values during an iteration of some sort. Pass in the first_value, the second_value, and the cardinality of the iteration.

@param first_value Object

@param second_value Object

@param iteration Integer

The number of times through the iteration.

@return Object

The first_value if iteration is even.  The second_value if
iteration is odd.
# File lib/metric_fu/templates/template.rb, line 218
def cycle(first_value, second_value, iteration)
  return first_value if iteration % 2 == 0
  second_value
end
display_location(location) click to toggle source
# File lib/metric_fu/templates/template.rb, line 159
def display_location(location)
  class_name, method_name = location.fetch("class_name"), location.fetch("method_name")
  str = ""
  str += link_to_filename(location.fetch("file_name"), location.fetch("line_number"))
  str += " : " if method_name || class_name
  if method_name
    str += "#{method_name}"
  else
    # TODO HOTSPOTS BUG ONLY exists on move over to metric_fu
    if class_name.is_a?(String)
      str += "#{class_name}"
    end
  end
  str
end
erb_template_source(template_file) click to toggle source
# File lib/metric_fu/templates/template.rb, line 39
def erb_template_source(template_file)
  erb_doc       = File.read(template_file)
  erb           = ERB.new(erb_doc)
  erb.filename  = template_file
  erb
end
erbify(section) click to toggle source

Creates a new erb evaluated result from the passed in section.

@param section String

The section name of

@return String

The erb evaluated string
# File lib/metric_fu/templates/template.rb, line 28
def erbify(section)
  template_file = template(section)
  erb           = erb_template_source(template_file)
  erb.result(binding)
rescue => e
  message = "Error: #{e.class}; message #{e.message}. "
  message << "Failed evaluating erb template "
  message << "for section #{section} and template #{template_file}."
  raise message
end
inline_css(css) click to toggle source

Returns the contents of a given css file in order to render it inline into a template.

@param css String

The name of a css file to open

@return String

The contents of the css file
# File lib/metric_fu/templates/template.rb, line 120
def inline_css(css)
  css_file = File.join(MetricFu.lib_dir, "templates", css)
  MetricFu::Utility.binread(css_file)
end
metric_template_path(metric) click to toggle source
# File lib/metric_fu/templates/template.rb, line 84
def metric_template_path(metric)
  File.join(MetricFu.metrics_dir, metric, "report.html.erb")
end
output_filename(section) click to toggle source

Returns the filename that the template will render into for a given section. In this case, the section name + '.html'

@param section String

A section of the template to render

@return String

The output filename
# File lib/metric_fu/templates/template.rb, line 108
def output_filename(section)
  section.to_s + ".html"
end
round_to_tenths(decimal) click to toggle source
# File lib/metric_fu/templates/template.rb, line 144
def round_to_tenths(decimal)
  decimal = 0.0 if decimal.to_s.eql?("NaN")
  (decimal * 10).round / 10.0
end
snake_case_to_title_case(string) click to toggle source
# File lib/metric_fu/templates/template.rb, line 239
def snake_case_to_title_case(string)
  string.split("_").collect { |word| word[0] = word[0..0].upcase; word }.join(" ")
end
template(section) click to toggle source

Generates the filename of the template file to load and evaluate. In this case, the path to the template directory + the section name + .html.erb

@param section String

A section of the template to render

@return String

A file path
# File lib/metric_fu/templates/template.rb, line 74
def template(section)
  # TODO: each MetricFu::Metric should know about its templates
  #  This class knows too much about the filesystem structure
  if MetricFu::Metric.enabled_metrics.map(&:name).include?(section) # expects a symbol
    metric_template_path(section.to_s)
  else
    File.join(template_directory,  section.to_s + ".html.erb")
  end
end
template_directory() click to toggle source
# File lib/metric_fu/templates/template.rb, line 243
def template_directory
  fail "subclasses must specify template_directory. Usually File.dirname(__FILE__)"
end
template_exists?(section) click to toggle source

Determines whether a template file exists for a given section of the full template.

@param section String

The section of the template to check against

@return Boolean

Does a template file exist for this section or not?
# File lib/metric_fu/templates/template.rb, line 96
def template_exists?(section)
  File.exist?(template(section))
end