Module: SinatraMore::RoutingPlugin
- Defined in:
- lib/sinatra_more/routing_plugin.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#map(*args, &block) ⇒ Object
map constructs a mapping between a named route and a specified alias the mapping url can contain url query parameters map(:accounts).to(‘/accounts/url’) map(:admin, :show).to(‘/admin/show/:id’) map(:admin) { |namespace| namespace.map(:show).to(‘/admin/show/:id’) }.
-
#namespace(name, &block) ⇒ Object
Used to define namespaced route configurations in order to group similar routes Class evals the routes but with the namespace assigned which will append to each route namespace(:admin) { get(:show) { “…” } }.
-
#route(verb, path, options = {}, &block) ⇒ Object
Hijacking route method in sinatra to replace a route alias (i.e :account) with the full url string mapping Supports namespaces by accessing the instance variable and appending this to the route alias name If the path is not a symbol, nothing is changed and the original route method is invoked.
Class Method Details
.registered(app) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/sinatra_more/routing_plugin.rb', line 8 def self.registered(app) # Named paths stores the named route aliases mapping to the url # i.e { [:account] => '/account/path', [:admin, :show] => '/admin/show/:id' } app.set :named_paths, {} app.set :app_name, app.name.underscore.to_sym unless app.respond_to?(:app_name) app.set :uri_root, '/' unless app.respond_to?(:uri_root) app.helpers SinatraMore::RoutingHelpers # map constructs a mapping between a named route and a specified alias # the mapping url can contain url query parameters # map(:accounts).to('/accounts/url') # map(:admin, :show).to('/admin/show/:id') # map(:admin) { |namespace| namespace.map(:show).to('/admin/show/:id') } def map(*args, &block) named_router = SinatraMore::NamedRoute.new(self, *args) block_given? ? block.call(named_router) : named_router end # Used to define namespaced route configurations in order to group similar routes # Class evals the routes but with the namespace assigned which will append to each route # namespace(:admin) { get(:show) { "..." } } def namespace(name, &block) original, @_namespace = @_namespace, name self.class_eval(&block) @_namespace = original end # Hijacking route method in sinatra to replace a route alias (i.e :account) with the full url string mapping # Supports namespaces by accessing the instance variable and appending this to the route alias name # If the path is not a symbol, nothing is changed and the original route method is invoked def route(verb, path, ={}, &block) if path.kind_of? Symbol route_name = [self.app_name, @_namespace, path].flatten.compact path = named_paths[route_name] raise RouteNotFound.new("Route alias #{route_name.inspect} is not mapped to a url") unless path end super verb, path, , &block end end |
Instance Method Details
#map(*args, &block) ⇒ Object
map constructs a mapping between a named route and a specified alias the mapping url can contain url query parameters map(:accounts).to(‘/accounts/url’) map(:admin, :show).to(‘/admin/show/:id’) map(:admin) { |namespace| namespace.map(:show).to(‘/admin/show/:id’) }
21 22 23 24 |
# File 'lib/sinatra_more/routing_plugin.rb', line 21 def map(*args, &block) named_router = SinatraMore::NamedRoute.new(self, *args) block_given? ? block.call(named_router) : named_router end |
#namespace(name, &block) ⇒ Object
Used to define namespaced route configurations in order to group similar routes Class evals the routes but with the namespace assigned which will append to each route namespace(:admin) { get(:show) { “…” } }
29 30 31 32 33 |
# File 'lib/sinatra_more/routing_plugin.rb', line 29 def namespace(name, &block) original, @_namespace = @_namespace, name self.class_eval(&block) @_namespace = original end |
#route(verb, path, options = {}, &block) ⇒ Object
Hijacking route method in sinatra to replace a route alias (i.e :account) with the full url string mapping Supports namespaces by accessing the instance variable and appending this to the route alias name If the path is not a symbol, nothing is changed and the original route method is invoked
38 39 40 41 42 43 44 45 |
# File 'lib/sinatra_more/routing_plugin.rb', line 38 def route(verb, path, ={}, &block) if path.kind_of? Symbol route_name = [self.app_name, @_namespace, path].flatten.compact path = named_paths[route_name] raise RouteNotFound.new("Route alias #{route_name.inspect} is not mapped to a url") unless path end super verb, path, , &block end |