Class: Metior::Report

Inherits:
Object show all
Defined in:
lib/metior/report.rb,
lib/metior/report/view.rb,
lib/metior/report/view_helper.rb,
reports/default.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, Sebastian Staudt

Direct Known Subclasses

Default

Defined Under Namespace

Modules: ViewHelper Classes: Default, View

Constant Summary collapse

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

Constructor Details

#initialize(repository, range = repository.vcs::DEFAULT_BRANCH) ⇒ Report

Creates a new report for the given repository and commit range

Parameters:

  • repository (Repository)

    The repository to analyze

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

    The commit range to analyze



87
88
89
90
# File 'lib/metior/report.rb', line 87

def initialize(repository, range = repository.vcs::DEFAULT_BRANCH)
  @range      = range
  @repository = repository
end

Instance Attribute Details

#rangeString, 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



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

def range
  @range
end

#repositoryRepository (readonly)

Returns the repository that should be analyzed by this report

Returns:

  • (Repository)

    The repository attached to this report



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

def repository
  @repository
end

Class Method Details

.create(name, repository, range = repository.vcs::DEFAULT_BRANCH) ⇒ Object

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.vcs::DEFAULT_BRANCH)

    The commit range to analyze



42
43
44
45
46
# File 'lib/metior/report.rb', line 42

def self.create(name, repository, range = repository.vcs::DEFAULT_BRANCH)
  require File.join(REPORTS_PATH, name.to_s)
  name = name.to_s.split('_').map { |n| n.capitalize }.join('')
  const_get(name.to_sym).new(repository, range)
end

.nameString

Returns the name of this report

Returns:

  • (String)

    The name of this report



51
52
53
# File 'lib/metior/report.rb', line 51

def self.name
  class_variable_get(:@@name).to_s
end

.pathString

Returns the file system path this report resides in

Returns:

  • (String)

    The path of this report



58
59
60
# File 'lib/metior/report.rb', line 58

def self.path
  File.join REPORTS_PATH, name
end

.template_pathString

Returns the file system path this report's templates reside in

Returns:

  • (String)

    The path of this report's templates



65
66
67
# File 'lib/metior/report.rb', line 65

def self.template_path
  File.join path, 'templates'
end

.view_pathString

Returns the file system path this report's views reside in

Returns:

  • (String)

    The path of this report's views



72
73
74
# File 'lib/metior/report.rb', line 72

def self.view_path
  File.join path, 'views'
end

.viewsObject

Returns the symbolic names of the main views this report consists of



79
80
81
# File 'lib/metior/report.rb', line 79

def self.views
  class_variable_get :@@views
end

Instance Method Details

#copy_assets(target_dir) ⇒ Object (private)

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

This will copy the contents of the images, javascript and stylesheets directories inside the report's path into the target directory.

Parameters:

  • target_dir (String)

    The target directory of the report



126
127
128
129
130
131
132
133
134
135
# File 'lib/metior/report.rb', line 126

def copy_assets(target_dir)
  FileUtils.mkdir_p target_dir

  %w{images javascripts stylesheets}.map do |type|
    File.join(self.class.path, type)
  end.each do |src|
    next unless File.directory? src
    FileUtils.cp_r src, target_dir
  end
end

#generate(target_dir) ⇒ Object

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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/metior/report.rb', line 98

def generate(target_dir)
  target_dir = File.expand_path target_dir
  copy_assets(target_dir)

  Mustache.template_path  = self.class.template_path
  Mustache.view_path      = self.class.view_path
  Mustache.view_namespace = self.class

  self.class.views.each do |view_name|
    file_name = File.join target_dir, view_name.to_s.downcase + '.html'
    begin
      output_file = File.open file_name, 'w'
      output_file.write Mustache.view_class(view_name).new(self).render
    ensure
      output_file.close
    end
  end
end