module Reviser::Helpers::Criteria

This module enables to imports automaticlly all modules for the analysis

Convention over configuration ! A analysis module contains the word ‘tool’ in its filename. You also have the possibility to put code in the ext folder.

@example Call a criterion during analysis (in the config File):

criteria:
  - :count_lines
  - :list_files
  - :<method>: <custom label>

In the last item of the list, the custom label will overwrite the label in labels.yml if it exist.

Constants

CRITERIA

Path of criteria

EXTENSIONS

Path of extensions

PWD

Where I am ?

Attributes

criteria[R]
output[R]

Public Instance Methods

call(meth) click to toggle source

Enable to call a specified method. @param meth [String] Method to call. @return results of the method.

# File lib/reviser/helpers/criteria.rb, line 76
def call meth
        if @criteria.key? meth
                @logger.h1(Logger::INFO, "Include methods of #{@criteria[meth]}") unless respond_to? meth
                self.class.send(:include, @criteria[meth]) unless respond_to? meth

                send meth
        else
                nil
        end
end

Protected Instance Methods

all() click to toggle source

Get all criteria which can be used. @return [Array] all criteria

# File lib/reviser/helpers/criteria.rb, line 92
def all
        @criteria.keys.map &:to_sym
end
camelize(file_module) click to toggle source

Gets the name of module @param file_module Name of the file module.

# File lib/reviser/helpers/criteria.rb, line 132
def camelize file_module
        file_module.split('_').each {|s| s.capitalize! }.join('')
end
create_label(meth) click to toggle source

Create label for a method. @param meth [String] method linked to the label @return [String] Renamed Label inspired of the name of the method

# File lib/reviser/helpers/criteria.rb, line 153
def create_label meth
        @logger.h2 Logger::ERROR, "Create label for #{meth}. You should custom your label (see 'reviser add')"
        meth.to_s.split('_').each {|s| s.capitalize! }.join(' ')
end
load_labels(key) click to toggle source

Load labels given by the user. If the label doesn’t exist, it will created with the name of the method. @param key Key of criteria in config file

# File lib/reviser/helpers/criteria.rb, line 139
def load_labels key
        labels = Labels.load

        if Cfg.has_key?(key) && Cfg[key].respond_to?('each')
                Cfg[key].each do |meth|
                        label = ((labels.respond_to?('each') && labels.key?(meth.to_sym))) && labels[meth.to_sym] || create_label(meth)
                        @output[meth.to_sym] = label
                end
        end
end
load_module_methods(module_name) click to toggle source
# File lib/reviser/helpers/criteria.rb, line 121
def load_module_methods module_name
                  mod = Object.const_get module_name, false

                  @logger.h3 Logger::INFO, "Load #{module_name}"

                  methods = mod.instance_methods false
                  methods.each { |method| populate(method, mod) }
end
load_modules(directory, regex = '*') click to toggle source

Load all of modules available for the analysis @param directory Directory where search of modules is done. @param regex regex to find name of modules.

# File lib/reviser/helpers/criteria.rb, line 107
  def load_modules directory, regex = '*'
          @logger.h2 Logger::INFO, "Modules of #{directory}"
          modules =  Dir[File.join(directory, regex)]

          namespace = directory == EXTENSIONS && 'Reviser::Extensions' || 'Reviser::Criteria'
          modules.each do |m|
                  require_relative m
                  ext = File.extname m
                  module_name = "#{namespace}::#{camelize(File.basename(m,ext))}"
                  
                  load_module_methods module_name
        end        
end
populate(criterion, module_name) click to toggle source
from Cfg file to symbols

@param criterion The criteria @param module_name The name of the module.

# File lib/reviser/helpers/criteria.rb, line 99
def populate criterion, module_name
        raise "Criterion '#{criterion}' is already defined in #{@criteria[criterion.to_sym]} (#{criterion}/#{module_name}).\nPlease change the name of the method in one of modules." if @criteria.has_key? criterion.to_sym
        @criteria[criterion.to_sym] = module_name
end