Class: Jets::Router::Help

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/jets/router/help.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Help

Returns a new instance of Help.



7
8
9
10
# File 'lib/jets/router/help.rb', line 7

def initialize(options={})
  @options = options
  @engines = {}
end

Instance Method Details

#all_routesObject



12
13
14
# File 'lib/jets/router/help.rb', line 12

def all_routes
  Jets::Router.routes
end

#any_mount?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/jets/router/help.rb', line 79

def any_mount?
  routes.any?(&:mount_class_name)
end

#collect_engine_routes(routes) ⇒ Object



27
28
29
30
31
# File 'lib/jets/router/help.rb', line 27

def collect_engine_routes(routes)
  routes.each do |route|
    collect_engine_routes_per(route) if route.engine?
  end
end

#collect_engine_routes_per(route) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/jets/router/help.rb', line 33

def collect_engine_routes_per(route)
  name = route.endpoint
  return unless route.engine?
  return if @engines[name]

  route_set = route.rack_app.route_set
  if route_set.is_a?(Jets::Router::RouteSet)
    @engines[name] = route_set.routes
  end
end

#filter(routes) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/jets/router/help.rb', line 44

def filter(routes)
  if @options[:controller]
    routes.select do |route|
      route.controller.include?(@options[:controller])
    end
  elsif @options[:grep]
    grep_pattern = Regexp.new(@options[:grep], 'i')
    proc = filter_proc(grep_pattern)
    routes.select(&proc)
  elsif @options[:reject]
    reject_pattern = Regexp.new(@options[:reject], 'i')
    proc = filter_proc(reject_pattern)
    routes.reject(&proc)
  else
    routes
  end
end

#filter_proc(pattern) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/jets/router/help.rb', line 62

def filter_proc(pattern)
  Proc.new do |route|
    route.as =~ pattern ||
    route.http_method =~ pattern ||
    route.path =~ pattern ||
    route.controller =~ pattern ||
    route.mount_class_name =~ pattern
  end
end

#format_with_newlines(line) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/jets/router/help.rb', line 115

def format_with_newlines(line)
  return '' if @options[:header] == false

  case @options[:format]
  when "markdown"
    # need leading newline for routes_table jets prints when route not found
    # Unsure why it needs to be here and not at the routes_table method.
    "\n#{line}\n\n"
  when "space"
    "#{line}\n\n"
  else
    "#{line}\n"
  end
end

#headerObject



72
73
74
75
76
77
# File 'lib/jets/router/help.rb', line 72

def header
  header = ["As (Prefix)", "Verb", "Path (URI Pattern)", "Controller#action"]
  # any_mount = routes.any?(&:mount_class_name)
  # header << "Mount" if any_mount
  header
end

#presenter_text(summary_line, routes) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/jets/router/help.rb', line 95

def presenter_text(summary_line, routes)
  text = format_with_newlines(summary_line)
  if routes.empty?
    text += "The routes table is empty.\n"
    return text
  end

  presenter = CliFormat::Presenter.new(@options)
  presenter.header = header unless @options[:header] == false
  routes.each do |route|
    row = [route.as, route.http_method, route.path, route.to]
    # row << route.mount_class_name if any_mount?
    presenter.rows << row
  end
  text += presenter.text.to_s # <Text::Table> => String
  text += "\n" unless text.ends_with?("\n")
  text += "\n" if @options[:format] == "space" # add another newline for space format
  text
end


83
84
85
# File 'lib/jets/router/help.rb', line 83

def print
  puts text
end

#routesObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/jets/router/help.rb', line 16

def routes
  routes = filter(all_routes)
  collect_engine_routes(routes)
  if ENV['JETS_ROUTES_INTERNAL']
    routes = routes.select { |route| route.internal }
  else
    routes = routes.reject { |route| route.internal }
  end
  routes
end

#textObject



87
88
89
90
91
92
93
# File 'lib/jets/router/help.rb', line 87

def text
  text = presenter_text("Routes for app:", routes)
  @engines.each do |name, routes|
    text += presenter_text("Routes for #{name}:", routes)
  end
  text
end