class Railroader::Checks

Collects up results from running different checks.

Checks can be added with +Check.add(check_class)+

All .rb files in checks/ will be loaded.

Attributes

checks_run[R]
controller_warnings[R]
model_warnings[R]
template_warnings[R]
warnings[R]

Public Class Methods

actually_run_checks(checks, check_runner, app_tree, tracker) click to toggle source
# File lib/railroader/checks.rb, line 119
def self.actually_run_checks(checks, check_runner, app_tree, tracker)
  threads = [] # Results for parallel
  results = [] # Results for sequential
  parallel = tracker.options[:parallel_checks]
  error_mutex = Mutex.new

  checks.each do |c|
    check_name = get_check_name c
    Railroader.notify " - #{check_name}"

    if parallel
      threads << Thread.new do
        self.run_a_check(c, error_mutex, app_tree, tracker)
      end
    else
      results << self.run_a_check(c, error_mutex, app_tree, tracker)
    end

    # Maintain list of which checks were run
    # mainly for reporting purposes
    check_runner.checks_run << check_name[5..-1]
  end

  threads.each { |t| t.join }

  Railroader.notify "Checks finished, collecting results..."

  if parallel
    threads.each do |thread|
      thread.value.each do |warning|
        check_runner.add_warning warning
      end
    end
  else
    results.each do |warnings|
      warnings.each do |warning|
        check_runner.add_warning warning
      end
    end
  end

  check_runner
end
add(klass) click to toggle source

Add a check. This will call klass.new when running tests

# File lib/railroader/checks.rb, line 16
def self.add klass
  @checks << klass unless @checks.include? klass
end
add_optional(klass) click to toggle source

Add an optional check

# File lib/railroader/checks.rb, line 21
def self.add_optional klass
  @optional_checks << klass unless @checks.include? klass
end
checks() click to toggle source
# File lib/railroader/checks.rb, line 25
def self.checks
  @checks + @optional_checks
end
initialize_checks(check_directory = "") click to toggle source
# File lib/railroader/checks.rb, line 33
def self.initialize_checks check_directory = ""
  # Load all files in check_directory
  Dir.glob(File.join(check_directory, "*.rb")).sort.each do |f|
    require f
  end
end
missing_checks(included_checks, excluded_checks) click to toggle source
# File lib/railroader/checks.rb, line 40
def self.missing_checks included_checks, excluded_checks
  included_checks = included_checks.map(&:to_s).to_set
  excluded_checks = excluded_checks.map(&:to_s).to_set

  if included_checks == Set['CheckNone']
    return []
  else
    loaded = self.checks.map { |name| name.to_s.gsub('Railroader::', '') }.to_set
    missing = (included_checks - loaded) + (excluded_checks - loaded)

    unless missing.empty?
      return missing
    end
  end

  []
end
new(options = { }) click to toggle source

No need to use this directly.

# File lib/railroader/checks.rb, line 59
def initialize options = { }
  if options[:min_confidence]
    @min_confidence = options[:min_confidence]
  else
    @min_confidence = Railroader.get_defaults[:min_confidence]
  end

  @warnings = []
  @template_warnings = []
  @model_warnings = []
  @controller_warnings = []
  @checks_run = []
end
optional_checks() click to toggle source
# File lib/railroader/checks.rb, line 29
def self.optional_checks
  @optional_checks
end
run_checks(app_tree, tracker) click to toggle source

Run all the checks on the given Tracker. Returns a new instance of Checks with the results.

# File lib/railroader/checks.rb, line 113
def self.run_checks(app_tree, tracker)
  checks = self.checks_to_run(tracker)
  check_runner = self.new :min_confidence => tracker.options[:min_confidence]
  self.actually_run_checks(checks, check_runner, app_tree, tracker)
end

Private Class Methods

checks_to_run(tracker) click to toggle source
# File lib/railroader/checks.rb, line 169
def self.checks_to_run tracker
  to_run = if tracker.options[:run_all_checks] or tracker.options[:run_checks]
             @checks + @optional_checks
           else
             @checks
           end

  self.filter_checks to_run, tracker
end
filter_checks(checks, tracker) click to toggle source
# File lib/railroader/checks.rb, line 179
def self.filter_checks checks, tracker
  skipped = tracker.options[:skip_checks]
  explicit = tracker.options[:run_checks]

  checks.reject do |c|
    check_name = self.get_check_name(c)

    skipped.include? check_name or
      (explicit and not explicit.include? check_name)
  end
end
get_check_name(check_class) click to toggle source
# File lib/railroader/checks.rb, line 165
def self.get_check_name check_class
  check_class.to_s.split("::").last
end
run_a_check(klass, mutex, app_tree, tracker) click to toggle source
# File lib/railroader/checks.rb, line 191
def self.run_a_check klass, mutex, app_tree, tracker
  check = klass.new(app_tree, tracker)

  begin
    check.run_check
  rescue => e
    mutex.synchronize do
      tracker.error e
    end
  end

  check.warnings
end

Public Instance Methods

add_warning(warning) click to toggle source

Add Warning to list of warnings to report. Warnings are split into four different arrays for template, controller, model, and generic warnings.

Will not add warnings which are below the minimum confidence level.

# File lib/railroader/checks.rb, line 78
def add_warning warning
  unless warning.confidence > @min_confidence
    case warning.warning_set
    when :template
      @template_warnings << warning
    when :warning
      @warnings << warning
    when :controller
      @controller_warnings << warning
    when :model
      @model_warnings << warning
    else
      raise "Unknown warning: #{warning.warning_set}"
    end
  end
end
all_warnings() click to toggle source

Return an array of all warnings found.

# File lib/railroader/checks.rb, line 107
def all_warnings
  @warnings + @template_warnings + @controller_warnings + @model_warnings
end
diff(other_checks) click to toggle source

Return a hash of arrays of new and fixed warnings

diff = checks.diff old_checks
diff[:fixed]  # [...]
diff[:new]    # [...]
# File lib/railroader/checks.rb, line 100
def diff other_checks
  my_warnings = self.all_warnings
  other_warnings = other_checks.all_warnings
  Railroader::Differ.new(my_warnings, other_warnings).diff
end