class Railroader::Tracker

The Tracker keeps track of all the processed information.

Constants

UNKNOWN_MODEL

Place holder when there should be a model, but it is not clear what model it will be.

Attributes

checks[RW]
config[RW]
constants[RW]
controllers[RW]
duration[RW]
end_time[RW]
errors[RW]
filter_cache[RW]
ignored_filter[RW]
initializers[RW]
libs[RW]
models[RW]
options[RW]
processor[RW]
routes[RW]
start_time[RW]
template_cache[RW]
templates[RW]

Public Class Methods

new(app_tree, processor = nil, options = {}) click to toggle source

Creates a new Tracker.

The Processor argument is only used by other Processors that might need to access it.

# File lib/railroader/tracker.rb, line 25
def initialize(app_tree, processor = nil, options = {})
  @app_tree = app_tree
  @processor = processor
  @options = options

  @config = Railroader::Config.new(self)
  @templates = {}
  @controllers = {}
  # Initialize models with the unknown model so
  # we can match models later without knowing precisely what
  # class they are.
  @models = {}
  @models[UNKNOWN_MODEL] = Railroader::Model.new(UNKNOWN_MODEL, nil, nil, nil, self)
  @routes = {}
  @initializers = {}
  @errors = []
  @libs = {}
  @constants = Railroader::Constants.new
  @checks = nil
  @processed = nil
  @template_cache = Set.new
  @filter_cache = {}
  @call_index = nil
  @start_time = Time.now
  @end_time = nil
  @duration = nil
end

Public Instance Methods

add_constant(name, value, context = nil) click to toggle source
# File lib/railroader/tracker.rb, line 193
def add_constant name, value, context = nil
  @constants.add name, value, context unless @options[:disable_constant_tracking]
end
app_path() click to toggle source
# File lib/railroader/tracker.rb, line 77
def app_path
  @app_path ||= File.expand_path @options[:app_path]
end
check_initializers(target, method) click to toggle source

Searches the initializers for a method call

# File lib/railroader/tracker.rb, line 159
def check_initializers target, method
  finder = Railroader::FindCall.new target, method, self

  initializers.sort.each do |name, initializer|
    finder.process_source initializer
  end

  finder.matches
end
constant_lookup(name) click to toggle source
# File lib/railroader/tracker.rb, line 197
def constant_lookup name
  @constants.get_literal name unless @options[:disable_constant_tracking]
end
each_class() { |src, set_name, file| ... } click to toggle source
# File lib/railroader/tracker.rb, line 116
def each_class
  classes = [self.controllers, self.models]

  if @options[:index_libs]
    classes << self.libs
  end

  classes.each do |set|
    set.each do |set_name, collection|
      collection.src.each do |file, src|
        yield src, set_name, file
      end
    end
  end
end
each_method() { |src, set_name, method_name, definition| ... } click to toggle source

Iterate over all methods in controllers and models.

# File lib/railroader/tracker.rb, line 82
def each_method
  classes = [self.controllers, self.models]

  if @options[:index_libs]
    classes << self.libs
  end

  classes.each do |set|
    set.each do |set_name, collection|
      collection.each_method do |method_name, definition|
        src = definition[:src]
        yield src, set_name, method_name, definition[:file]
      end
    end
  end
end
each_template() { |k, templates| ... } click to toggle source

Iterates over each template, yielding the name and the template. Prioritizes templates which have been rendered.

# File lib/railroader/tracker.rb, line 101
def each_template
  if @processed.nil?
    @processed, @rest = templates.keys.sort_by{|template| template.to_s}.partition { |k| k.to_s.include? "." }
  end

  @processed.each do |k|
    yield k, templates[k]
  end

  @rest.each do |k|
    yield k, templates[k]
  end
end
error(exception, backtrace = nil) click to toggle source

Add an error to the list. If no backtrace is given, the one from the exception will be used.

# File lib/railroader/tracker.rb, line 55
def error exception, backtrace = nil
  backtrace ||= exception.backtrace
  unless backtrace.is_a? Array
    backtrace = [ backtrace ]
  end

  Railroader.debug exception
  Railroader.debug backtrace

  @errors << { :error => exception.to_s.gsub("\n", " "), :backtrace => backtrace }
end
filtered_warnings() click to toggle source
# File lib/railroader/tracker.rb, line 178
def filtered_warnings
  if self.ignored_filter
    self.warnings.reject do |w|
      self.ignored_filter.ignored? w
    end
  else
    self.warnings
  end
end
find_call(options) click to toggle source

Find a method call.

Options:

* :target => target name(s)
* :method => method name(s)
* :chained => search in method chains

If :target => false or :target => nil, searches for methods without a target. Targets and methods can be specified as a symbol, an array of symbols, or a regular expression.

If :chained => true, matches target at head of method chain and method at end.

For example:

find_call :target => User, :method => :all, :chained => true

could match

User.human.active.all(...)
# File lib/railroader/tracker.rb, line 153
def find_call options
  index_call_sites unless @call_index
  @call_index.find_calls options
end
find_class(name) click to toggle source
# File lib/railroader/tracker.rb, line 201
def find_class name
  [@controllers, @models, @libs].each do |collection|
    if c = collection[name]
      return c
    end
  end

  nil
end
index_call_sites() click to toggle source
# File lib/railroader/tracker.rb, line 211
def index_call_sites
  finder = Railroader::FindAllCalls.new self

  self.each_method do |definition, set_name, method_name, file|
    finder.process_source definition, :class => set_name, :method => method_name, :file => file
  end

  self.each_class do |definition, set_name, file|
    finder.process_source definition, :class => set_name, :file => file
  end

  self.each_template do |_name, template|
    finder.process_source template.src, :template => template, :file => template.file
  end

  @call_index = Railroader::CallIndex.new finder.calls
end
reindex_call_sites(locations) click to toggle source

Reindex call sites

Takes a set of symbols which can include :templates, :models, or :controllers

This will limit reindexing to the given sets

# File lib/railroader/tracker.rb, line 235
def reindex_call_sites locations
  # If reindexing templates, models, and controllers, just redo
  # everything
  if locations.length == 3
    return index_call_sites
  end

  if locations.include? :templates
    @call_index.remove_template_indexes
  end

  classes_to_reindex = Set.new
  method_sets = []

  if locations.include? :models
    classes_to_reindex.merge self.models.keys
    method_sets << self.models
  end

  if locations.include? :controllers
    classes_to_reindex.merge self.controllers.keys
    method_sets << self.controllers
  end

  @call_index.remove_indexes_by_class classes_to_reindex

  finder = Railroader::FindAllCalls.new self

  method_sets.each do |set|
    set.each do |set_name, info|
      info.each_method do |method_name, definition|
        src = definition[:src]
        finder.process_source src, :class => set_name, :method => method_name, :file => definition[:file]
      end
    end
  end

  if locations.include? :templates
    self.each_template do |_name, template|
      finder.process_source template.src, :template => template, :file => template.file
    end
  end

  @call_index.index_calls finder.calls
end
report() click to toggle source

Returns a Report with this Tracker's information

# File lib/railroader/tracker.rb, line 170
def report
  Railroader::Report.new(@app_tree, self)
end
reset_controller(path) click to toggle source
# File lib/railroader/tracker.rb, line 334
def reset_controller path
  controller_name = nil

  # Remove from controller
  @controllers.each do |name, controller|
    if controller.files.include?(path)
      controller_name = name

      # Remove templates rendered from this controller
      @templates.each do |template_name, template|
        if template.render_path and template.render_path.include_controller? name
          reset_template template_name
          @call_index.remove_template_indexes template_name
        end
      end

      # Remove calls indexed from this controller
      @call_index.remove_indexes_by_class [name]
      break
    end
  end
  @controllers.delete controller_name
end
reset_lib(path) click to toggle source

Clear information related to model

# File lib/railroader/tracker.rb, line 321
def reset_lib path
  lib_name = nil

  @libs.each do |name, lib|
    if lib.files.include?(path)
      lib_name = name
      break
    end
  end

  @libs.delete lib_name
end
reset_model(path) click to toggle source

Clear information related to model

# File lib/railroader/tracker.rb, line 307
def reset_model path
  model_name = nil

  @models.each do |name, model|
    if model.files.include?(path)
      model_name = name
      break
    end
  end

  @models.delete model_name
end
reset_routes() click to toggle source

Clear information about routes

# File lib/railroader/tracker.rb, line 359
def reset_routes
  @routes = {}
end
reset_template(name) click to toggle source

Clear information related to template

# File lib/railroader/tracker.rb, line 298
def reset_template name
  name = name.to_sym
  @templates.delete name
  @processed = nil
  @rest = nil
  @template_cache.clear
end
reset_templates(options = { :only_rendered => false }) click to toggle source

Clear information related to templates. If :only_rendered => true, will delete templates rendered from controllers (but not those rendered from other templates)

# File lib/railroader/tracker.rb, line 284
def reset_templates options = { :only_rendered => false }
  if options[:only_rendered]
    @templates.delete_if do |_name, template|
      template.rendered_from_controller?
    end
  else
    @templates = {}
  end
  @processed = nil
  @rest = nil
  @template_cache.clear
end
run_checks() click to toggle source

Run a set of checks on the current information. Results will be stored in Tracker#checks.

# File lib/railroader/tracker.rb, line 69
def run_checks
  @checks = Railroader::Checks.run_checks(@app_tree, self)

  @end_time = Time.now
  @duration = @end_time - @start_time
  @checks
end
unused_fingerprints() click to toggle source
# File lib/railroader/tracker.rb, line 188
def unused_fingerprints
  return [] unless self.ignored_filter
  self.ignored_filter.obsolete_fingerprints
end
warnings() click to toggle source
# File lib/railroader/tracker.rb, line 174
def warnings
  self.checks.all_warnings
end