Class: Rutter::Builder
- Inherits:
-
Object
- Object
- Rutter::Builder
- Defined in:
- lib/rutter/builder.rb
Overview
The router redirect incoming requests to defined endpoints. Most often it's to a controller and an action or a Rack endpoint.
Instance Attribute Summary collapse
-
#flat_map ⇒ Array<Rutter::Route>
readonly
Defined routes.
-
#named_map ⇒ Hash<Symbol => Rutter::Route>
readonly
Named routes.
-
#verb_map ⇒ Hash<String => Array>
readonly
Routes group by verb.
Instance Method Summary collapse
-
#add(method, path, to:, as: nil) ⇒ Rutter::Route
Adds a new route to the collection.
-
#freeze ⇒ self
Freezes the router and its routes.
-
#initialize(scheme: "http", host: "example.com", port: 80, controller_suffix: nil) { ... } ⇒ self
constructor
Initializes the router.
-
#mount(app, at:) ⇒ void
Mount a Rack application at the specified path.
-
#path(name, params = {}) ⇒ String
Transforms a named route into a URL path.
-
#redirect(destination, status: 302) ⇒ Proc
Convenient method to create a redirect endpoint.
-
#root(**opts) ⇒ Rutter::Route
Defines a root route (a GET route for '/').
-
#scope(**opts) { ... } ⇒ Rutter::Scope
Starts a scoped collection of routes.
-
#url(name, params = {}) ⇒ String
Transforms a named route into a full URL with scheme and host.
Constructor Details
#initialize(scheme: "http", host: "example.com", port: 80, controller_suffix: nil) { ... } ⇒ self
Initializes the router.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rutter/builder.rb', line 48 def initialize( scheme: "http", host: "example.com", port: 80, controller_suffix: nil, &block ) @scheme = scheme @host = host @port = port @controller_suffix = controller_suffix @flat_map = [] @verb_map = Hash.new { |hash, key| hash[key] = [] } @named_map = {} instance_eval(&block) if block_given? end |
Instance Attribute Details
#flat_map ⇒ Array<Rutter::Route> (readonly)
Defined routes.
30 31 32 |
# File 'lib/rutter/builder.rb', line 30 def flat_map @flat_map end |
#named_map ⇒ Hash<Symbol => Rutter::Route> (readonly)
Named routes.
30 31 32 |
# File 'lib/rutter/builder.rb', line 30 def named_map @named_map end |
#verb_map ⇒ Hash<String => Array> (readonly)
Routes group by verb.
30 31 32 |
# File 'lib/rutter/builder.rb', line 30 def verb_map @verb_map end |
Instance Method Details
#add(method, path, to:, as: nil) ⇒ Rutter::Route
Adds a new route to the collection.
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/rutter/builder.rb', line 122 def add(method, path, to:, as: nil) route = Route.new(method, path, to, controller_suffix: @controller_suffix) flat_map << route verb_map[route.method] << route return route unless as add_named_route!(as, route) end |
#freeze ⇒ self
Freezes the router and its routes.
247 248 249 250 251 252 253 |
# File 'lib/rutter/builder.rb', line 247 def freeze flat_map.freeze verb_map.freeze named_map.freeze super end |
#mount(app, at:) ⇒ void
This method returns an undefined value.
Mount a Rack application at the specified path.
74 75 76 |
# File 'lib/rutter/builder.rb', line 74 def mount(app, at:) Route::VERBS.each { |verb| add verb, "#{at}*_", to: app } end |
#path(name, params = {}) ⇒ String
Transforms a named route into a URL path.
183 184 185 186 187 188 189 190 191 |
# File 'lib/rutter/builder.rb', line 183 def path(name, params = {}) route = named_map[name] raise ArgumentError, "no route called '#{name}'" unless route path = route.(params) path = path.gsub(%r{\A[\/]+|[\/]+\z}, "") "/#{path}" end |
#redirect(destination, status: 302) ⇒ Proc
Convenient method to create a redirect endpoint.
97 98 99 |
# File 'lib/rutter/builder.rb', line 97 def redirect(destination, status: 302) ->(_env) { [status, { "Location" => destination.to_s }, []] } end |
#root(**opts) ⇒ Rutter::Route
Defines a root route (a GET route for '/').
106 107 108 |
# File 'lib/rutter/builder.rb', line 106 def root(**opts) get "/", **opts.merge(as: :root) end |
#scope(**opts) { ... } ⇒ Rutter::Scope
Starts a scoped collection of routes.
168 169 170 |
# File 'lib/rutter/builder.rb', line 168 def scope(**opts, &block) Scope.new(self, **opts, &block) end |
#url(name, params = {}) ⇒ String
Transforms a named route into a full URL with scheme and host.
211 212 213 214 215 216 217 218 219 |
# File 'lib/rutter/builder.rb', line 211 def url(name, params = {}) scheme = params.delete(:_scheme) || @scheme host = params.delete(:_host) || @host port = (params.delete(:_port) || @port).to_i host = "#{host}:#{port}" unless [80, 443].include?(port) subdomain = params.delete(:_subdomain) host = "#{subdomain}.#{host}" if subdomain "#{scheme}://#{host}#{path(name, params)}" end |