Class: Metior::Report::View

Inherits:
Mustache
  • Object
show all
Includes:
ViewHelper
Defined in:
lib/metior/report/view.rb

Overview

This class is an extended Mustache view

A view represents a whole page or a section of a page that displays information about a repository. It is always attached to a specific report and can access the information of the report and the repository.

Author:

  • Sebastian Staudt

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ViewHelper

#count, #even_odd, #reset_count

Constructor Details

#initialize(report) ⇒ View

Initializes this view with the given report

Parameters:

  • report (Report)

    The report this view belongs to



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

def initialize(report)
  @report = report
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

This will try to render a view as a partial of the current view or call a method of the repository

The partial view will either be aquired from the current view namespace, i.e. the report this view belongs to, or from the default report.

Parameters:

  • name (Symbol)

    The name of the view to render or the repository method to call

  • args (Object, ...)

    The arguments to pass to the method

  • block (Proc)

    The block to pass to the method

See Also:



67
68
69
70
71
72
# File 'lib/metior/report/view.rb', line 67

def method_missing(name, *args, &block)
  view_class = Mustache.view_class name
  return view_class.new(@report).render if view_class != Mustache

  repository.send name, *args, &block
end

Class Method Details

.inherited(subclass) ⇒ Object

This will initialize new view classes

Parameters:

  • subclass (Class)

    The inheriting view class



24
25
26
# File 'lib/metior/report/view.rb', line 24

def self.inherited(subclass)
  subclass.send :class_variable_set, :@@required_features, []
end

.requires(*features) ⇒ Object

Specifies one or more VCS features that are required to generate this view

Examples:

class LineStatsView < View

  requires :line_stats

  ...

end

Parameters:

  • features (Symbol, ...)

    One ore more features that are required for this view



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

def self.requires(*features)
  required_features = class_variable_get :@@required_features
  required_features += features
  class_variable_set :@@required_features, required_features
end

Instance Method Details

#render(*args) ⇒ Object

This checks if all required VCS features of this view are available for this report's repository

Parameters:

  • args (Object, ...)

    The arguments expected by Mustache#render

See Also:



81
82
83
84
# File 'lib/metior/report/view.rb', line 81

def render(*args)
  features = self.class.send :class_variable_get, :@@required_features
  super if features.all? { |feature| repository.supports? feature }
end

#repositoryRepository

Returns the repository that is analyzed by the report this view belongs to

Returns:

  • (Repository)

    The repository belonging to this view's report



90
91
92
# File 'lib/metior/report/view.rb', line 90

def repository
  @report.repository
end

#respond_to?(name) ⇒ Boolean

Returns whether the given name refers a partial view that can be rendered or method that can be called

This checks whether this view has a method with the given name, or if another view with this name exists, or if the repository has a method with this name.

Parameters:

  • name (Symbol)

    The name of the partial or method

Returns:

  • (Boolean)

    true if the given name refers a partial or method

See Also:



105
106
107
108
109
# File 'lib/metior/report/view.rb', line 105

def respond_to?(name)
  methods.include?(name.to_s) ||
  Mustache.view_class(name) != Mustache ||
  repository.respond_to?(name)
end

#vcs_nameSymbol

Returns the name of the VCS the analyzed repository is using

Returns:

  • (Symbol)

    The name of the current VCS



114
115
116
# File 'lib/metior/report/view.rb', line 114

def vcs_name
  repository.vcs::NAME
end