class RouteCounter::ConsoleFormatter

Public Class Methods

new(path_hash) click to toggle source
# File lib/route_counter/console_formatter.rb, line 16
def initialize(path_hash)
  @buffer = []
  @path_hash = path_hash || {}
end
puts!(recorder_klass) click to toggle source
# File lib/route_counter/console_formatter.rb, line 6
def puts!(recorder_klass)
  # from railties/routes.rake
  all_routes = Rails.application.routes.routes
  inspector = ActionDispatch::Routing::RoutesInspector.new(all_routes)
  path_hash = recorder_klass.actions_visited
  formatter = RouteCounter::ConsoleFormatter.new(path_hash)
  puts inspector.format(formatter, ENV['CONTROLLER'])
end

Public Instance Methods

header(routes) click to toggle source
# File lib/route_counter/console_formatter.rb, line 33
def header(routes)
  @buffer << draw_header(routes)
end
no_routes() click to toggle source
# File lib/route_counter/console_formatter.rb, line 37
    def no_routes
      @buffer << <<-MESSAGE.strip_heredoc
        You don't have any routes defined!

        Please add some routes in config/routes.rb.

        For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html.
        MESSAGE
    end
result() click to toggle source
# File lib/route_counter/console_formatter.rb, line 21
def result
  @buffer.join("\n")
end
section(routes) click to toggle source
# File lib/route_counter/console_formatter.rb, line 29
def section(routes)
  @buffer << draw_section(routes)
end
section_title(title) click to toggle source
# File lib/route_counter/console_formatter.rb, line 25
def section_title(title)
  @buffer << "\n#{title}:"
end

Private Instance Methods

count_for_route(r) click to toggle source
# File lib/route_counter/console_formatter.rb, line 73
def count_for_route(r)
  has_action = r[:reqs].to_s
  return "~" unless has_action.include?("#")
  count = 0
  @path_hash.each do |action, num|
    next unless has_action.start_with?(action)
    count += num
  end
  count.to_s
end
draw_header(routes) click to toggle source
# File lib/route_counter/console_formatter.rb, line 56
def draw_header(routes)
  count_width, name_width, verb_width, path_width = widths(routes)

  "#{"Count".rjust(count_width)} #{"Prefix".ljust(name_width)} #{"Verb".ljust(verb_width)} #{"URI Pattern".ljust(path_width)} Controller#Action"
end
draw_section(routes) click to toggle source
# File lib/route_counter/console_formatter.rb, line 48
def draw_section(routes)
  count_width, name_width, verb_width, path_width = widths(routes)

  routes.map do |r|
    "#{count_for_route(r).ljust(count_width)} #{r[:name].ljust(name_width)} #{r[:verb].ljust(verb_width)} #{r[:path].ljust(path_width)} #{r[:reqs]}"
  end
end
max_count_width() click to toggle source
# File lib/route_counter/console_formatter.rb, line 69
def max_count_width
  @max_count_width ||= [@path_hash.values.max.to_s.length, "Count".length].max
end
widths(routes) click to toggle source
# File lib/route_counter/console_formatter.rb, line 62
def widths(routes)
  [ max_count_width,
   routes.map { |r| r[:name].length }.max,
   routes.map { |r| r[:verb].length }.max,
   routes.map { |r| r[:path].length }.max]
end