Module: Metior::Report

Included in:
Default
Defined in:
lib/metior/report.rb,
lib/metior/report/view.rb,
lib/metior/report/view_helper.rb

Overview

This code is free software; you can redistribute it and/or modify it under the terms of the new BSD License.

Copyright (c) 2011-2012, Sebastian Staudt

Defined Under Namespace

Modules: ClassMethods, ViewHelper Classes: Default, View

Constant Summary

REPORTS_PATH =

The path where the reports bundled with Metior live

File.expand_path File.join File.dirname(__FILE__), '..', '..', 'reports'

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (CommitCollection) commits (readonly)

Returns the commits analyzed by this report

Returns:



29
30
31
# File 'lib/metior/report.rb', line 29

def commits
  @commits
end

- (String, Range) range (readonly)

Returns the range of commits that should be analyzed by this report

Returns:

  • (String, Range)

    The range of commits covered by this report



34
35
36
# File 'lib/metior/report.rb', line 34

def range
  @range
end

- (Repository) repository (readonly)

Returns the repository that should be analyzed by this report

Returns:

  • (Repository)

    The repository attached to this report



39
40
41
# File 'lib/metior/report.rb', line 39

def repository
  @repository
end

Class Method Details

+ (Report) create(name, repository, range = repository.current_branch)

Create a new report instance for the given report name, repository and commit range

Parameters:

  • name (String, Symbol)

    The name of the report to load and initialize

  • repository (Repository)

    The repository to analyze

  • range (String, Range) (defaults to: repository.current_branch)

    The commit range to analyze

Returns:

  • (Report)

    The requested report



126
127
128
# File 'lib/metior/report.rb', line 126

def self.create(name, repository, range = repository.current_branch)
  Metior.find_report(name).new(repository, range)
end

+ (Object) included(mod)

Automatically registers a new report subclass as an available report type

This will also set the path of the new report class, so it can find its views, templates and assets.

Parameters:

  • mod (Module)

    A report subclass



136
137
138
139
140
141
# File 'lib/metior/report.rb', line 136

def self.included(mod)
  mod.extend ClassMethods
  mod.extend Metior::Registerable
  base_path = File.dirname caller.first.split(':').first
  mod.send :class_variable_set, :@@base_path, base_path
end

Instance Method Details

- (Object) copy_assets(target_dir) (private)

Copies the assets coming with this report to the given target directory

This will copy the files and directories that have been specified for the report from the report's path (or the report's ancestors) into the target directory.

Parameters:

  • target_dir (String)

    The target directory of the report

See Also:

  • assets


221
222
223
224
225
226
227
228
229
230
# File 'lib/metior/report.rb', line 221

def copy_assets(target_dir)
  FileUtils.mkdir_p target_dir

  self.class.assets.map do |asset|
    asset_path = self.class.find asset
    asset_dir = File.join target_dir, File.dirname(asset)
    FileUtils.mkdir_p asset_dir unless File.exists? asset_dir
    FileUtils.cp_r asset_path, asset_dir
  end
end

- (Object) generate(target_dir, with_assets = true)

Generates this report's output into the given target directory

This will generate individual HTML files for the main views of the report.

Parameters:

  • target_dir (String)

    The target directory of the report

  • with_assets (Boolean) (defaults to: true)

    If false the report's assets will not be copied to the target directory



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/metior/report.rb', line 163

def generate(target_dir, with_assets = true)
  target_dir = File.expand_path target_dir
  copy_assets target_dir if with_assets

  render.each do |view_name, output|
    file_name = File.join target_dir, view_name.to_s.downcase + '.html'
    begin
      output_file = File.open file_name, 'wb'
      output_file.write output
    ensure
      output_file.close
    end
  end
end

- (Object) init

This method is abstract.

Override this method to customize the initial setup of a report

Initializes a new report instance

This can be used to gather initial data, e.g. used by multiple views.



183
184
# File 'lib/metior/report.rb', line 183

def init
end

- (Report) initialize(repository, range = repository.current_branch)

Creates a new report for the given repository and commit range

Parameters:

  • repository (Repository)

    The repository to analyze

  • range (String, Range) (defaults to: repository.current_branch)

    The commit range to analyze

Returns:

  • (Report)

    a new instance of Report



147
148
149
150
151
152
153
# File 'lib/metior/report.rb', line 147

def initialize(repository, range = repository.current_branch)
  @range      = range
  @repository = repository
  @commits    = repository.commits range

  init
end

- (Hash<Symbol, String>) render

Renders the views of this report (or the its ancestors) and returns them in a hash

Returns:

  • (Hash<Symbol, String>)

    The names of the views and the corresponding rendered content



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/metior/report.rb', line 191

def render
  Mustache.view_namespace = self.class

  result = {}
  self.class.views.each do |view_name|
    template = File.join 'templates', "#{view_name}.mustache"
    template_path = self.class.find template

    view = File.join 'views', "#{view_name}.rb"
    view_path = self.class.find view

    Mustache.template_path = File.dirname template_path
    Mustache.view_path     = File.dirname view_path
    mustache = Mustache.view_class(view_name).new(self)
    mustache.template_name = view_name
    result[view_name] = mustache.render
  end
  result
end