Class: Railbow::Demo::RoutesDemo

Inherits:
Object
  • Object
show all
Defined in:
lib/railbow/demo/routes_demo.rb

Constant Summary collapse

VERB_COLORS =
{
  "GET" => Railbow::Formatters::Base::GREEN,
  "POST" => Railbow::Formatters::Base::YELLOW,
  "PATCH" => Railbow::Formatters::Base::CYAN,
  "PUT" => Railbow::Formatters::Base::CYAN,
  "DELETE" => Railbow::Formatters::Base::RED
}.freeze
RESET =
Railbow::Formatters::Base::RESET
BOLD =
Railbow::Formatters::Base::BOLD
DIM =
Railbow::Formatters::Base::DIM
CYAN =
Railbow::Formatters::Base::CYAN

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.runObject



24
25
26
# File 'lib/railbow/demo/routes_demo.rb', line 24

def self.run
  new.run
end

Instance Method Details

#runObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/railbow/demo/routes_demo.rb', line 28

def run
  routes = Fixtures::ROUTES
  groups = routes.group_by { |r| r[:reqs].include?("#") ? r[:reqs].split("#").first : "(other)" }

  lines = []
  groups.each do |label, group_routes|
    lines << ""
    lines << "#{BOLD}#{CYAN}\u2500\u2500 #{label} \u2500\u2500#{RESET}"

    columns = [
      Railbow::Table::Column.new(label: "Verb", sticky: true),
      Railbow::Table::Column.new(label: "URI Pattern", sticky: true),
      Railbow::Table::Column.new(label: "Controller#Action"),
      Railbow::Table::Column.new(label: "Prefix")
    ]

    rows = group_routes.map { |r|
      [
        colorize_verb(r[:verb]),
        colorize_path(r[:path]),
        colorize_reqs(r[:reqs]),
        r[:name].empty? ? "" : "#{DIM}#{r[:name]}#{RESET}"
      ]
    }

    renderer = Railbow::Table::Renderer.new(
      columns: columns,
      theme: Railbow::Table::Themes::PLAIN,
      compact: {oneline: false, dense: false, noheader: false, maxw: nil, hidden_columns: []},
      aliases: Railbow::Config.table_aliases
    )
    lines << renderer.render(rows)
  end

  puts lines.join("\n")
end