Class: Jets::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/router.rb

Constant Summary collapse

@@drawn_router =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



6
7
8
# File 'lib/jets/router.rb', line 6

def initialize
  @routes = []
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



5
6
7
# File 'lib/jets/router.rb', line 5

def routes
  @routes
end

Class Method Details

.all_pathsObject

Returns all paths including subpaths. Example: Input: [“posts/:id/edit”] Output: [“posts”, “posts/:id”, “posts/:id/edit”]



100
101
102
# File 'lib/jets/router.rb', line 100

def self.all_paths
  drawn_router.all_paths
end

.all_routes_validObject



115
116
117
# File 'lib/jets/router.rb', line 115

def self.all_routes_valid
  invalid_routes.empty?
end

.drawObject

Class methods



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

def self.draw
  drawn_router
end

.drawn_routerObject



85
86
87
88
89
90
# File 'lib/jets/router.rb', line 85

def self.drawn_router
  return @@drawn_router if @@drawn_router

  router = Jets.application.routes
  @@drawn_router = router
end

.has_controller?(name) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/jets/router.rb', line 75

def self.has_controller?(name)
  routes.detect { |r| r.controller_name == name }
end

.invalid_routesObject



119
120
121
# File 'lib/jets/router.rb', line 119

def self.invalid_routes
  routes.select { |r| !r.valid? }
end

.routesObject



92
93
94
# File 'lib/jets/router.rb', line 92

def self.routes
  drawn_router.routes
end

.routes_helpObject



104
105
106
107
108
109
110
111
112
113
# File 'lib/jets/router.rb', line 104

def self.routes_help
  return "Your routes table is empty." if routes.empty?

  table = Text::Table.new
  table.head = %w[Verb Path Controller#action]
  routes.each do |route|
    table.rows << [route.method, route.path, route.to]
  end
  table
end

Instance Method Details

#all_pathsObject

Useful for creating API Gateway Resources



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/jets/router.rb', line 42

def all_paths
  results = []
  paths = routes.map(&:path)
  paths.each do |p|
    sub_paths = []
    parts = p.split('/')
    until parts.empty?
      parts.pop
      sub_path = parts.join('/')
      sub_paths << sub_path unless sub_path == ''
    end
    results += sub_paths
  end
  @all_paths = (results + paths).sort.uniq
end

#create_route(options) ⇒ Object



32
33
34
# File 'lib/jets/router.rb', line 32

def create_route(options)
  @routes << Route.new(options)
end

#draw(&block) ⇒ Object



10
11
12
# File 'lib/jets/router.rb', line 10

def draw(&block)
  instance_eval(&block)
end

#ordered_routesObject

Useful for RouterMatcher

Precedence:

  1. Routes with no captures get highest precedence: posts/new

  2. Then consider the routes with captures: post/:id

  3. Last consider the routes with wildcards: *catchall

Within these 2 groups we consider the routes with the longest path first since posts/:id and posts/:id/edit can both match.



67
68
69
70
71
72
73
# File 'lib/jets/router.rb', line 67

def ordered_routes
  length = Proc.new { |r| r.path.length * -1 }
  capture_routes = routes.select { |r| r.path.include?(':') }.sort_by(&length)
  wildcard_routes = routes.select { |r| r.path.include?('*') }.sort_by(&length)
  simple_routes = (routes - capture_routes - wildcard_routes).sort_by(&length)
  simple_routes + capture_routes + wildcard_routes
end

#resources(name) ⇒ Object

resources macro expands to all the routes



22
23
24
25
26
27
28
29
30
# File 'lib/jets/router.rb', line 22

def resources(name)
  get "#{name}", to: "#{name}#index"
  get "#{name}/new", to: "#{name}#new"
  get "#{name}/:id", to: "#{name}#show"
  post "#{name}", to: "#{name}#create"
  get "#{name}/:id/edit", to: "#{name}#edit"
  put "#{name}/:id", to: "#{name}#update"
  delete "#{name}/:id", to: "#{name}#delete"
end

#root(to) ⇒ Object

root “posts#index”



37
38
39
# File 'lib/jets/router.rb', line 37

def root(to)
  @routes << Route.new(path: '', to: to, method: :get, root: true)
end