Module: Sinatra::Namespace

Defined in:
lib/sinatra/namespace.rb

Overview

Sinatra::Namespace

Sinatra::Namespace is an extension that adds namespaces to an application. This namespaces will allow you to share a path prefix for the routes within the namespace, and define filters, conditions and error handlers exclusively for them. Besides that, you can also register helpers and extensions that will be used only within the namespace.

Usage

Once you have loaded the extension (see below), you can use the namespace method to define namespaces in your application.

You can define a namespace by a path prefix:

namespace '/blog' do
  get { haml :blog }
  get '/:entry_permalink' do
    @entry = Entry.find_by_permalink!(params[:entry_permalink])
    haml :entry
  end

  # More blog routes...
end

by a condition:

namespace :host_name => 'localhost' do
  get('/admin/dashboard') { haml :dashboard }
  get('/admin/login')     { haml :login }

  # More admin routes...
end

or both:

namespace '/admin', :host_name => 'localhost' do
  get('/dashboard')  { haml :dashboard }
  get('/login')      { haml :login }
  post('/login')     {  }

  # More admin routes...
end

Regex is also accepted:

namespace /\/posts\/([^\/&?]+)\// do
  get { haml :blog }

  # More blog routes...
end

When you define a filter or an error handler, or register an extension or a set of helpers within a namespace, they only affect the routes defined in it. For instance, lets define a before filter to prevent the access of unauthorized users to the admin section of the application:

namespace '/admin' do
  helpers AdminHelpers
  before  { authenticate unless request.path_info == '/admin/login' }

  get '/dashboard' do
    # Only authenticated users can access here...
    haml :dashboard
  end

  # More admin routes...
end

get '/' do
  # Any user can access here...
  haml :index
end

Well, they actually also affect the nested namespaces:

namespace '/admin' do
  helpers AdminHelpers
  before  { authenticate unless request.path_info == '/admin/login' }

  namespace '/users' do
    get do
      # Only authenticated users can access here...
      @users = User.all
      haml :users
    end

    # More user admin routes...
  end

  # More admin routes...
end

Redirecting within the namespace can be done using redirect_to:

namespace '/admin' do
  get '/foo'  do
    redirect_to '/bar' # Redirects to /admin/bar
  end

  get '/foo' do
    redirect '/bar' # Redirects to /bar
  end
end

Classic Application Setup

To be able to use namespaces in a classic application all you need to do is require the extension:

require "sinatra"
require "sinatra/namespace"

namespace '/users' do
end

Modular Application Setup

To be able to use namespaces in a modular application all you need to do is require the extension, and then, register it:

require "sinatra/base"
require "sinatra/namespace"

class MyApp < Sinatra::Base
  register Sinatra::Namespace

  namespace '/users' do
  end
end

Within an extension

To be able to use namespaces within an extension, you need to first create an extension. This includes defining the ‘registered(app)` method in the module.

require 'sinatra/base' # For creating Sinatra extensions
require 'sinatra/namespace' # To create namespaces

module Zomg # Keep everything under "Zomg" namespace for sanity
  module Routes # Define a new "Routes" module

    def self.registered(app)
      # First, register the Namespace extension
      app.register Sinatra::Namespace

      # This defines an `/api` namespace on the application
      app.namespace '/api' do
        get '/users' do
          # do something with `GET "/api/users"`
        end
      end

    end
  end

  # Lastly, register the extension to use in our app
  Sinatra.register Routes
end

In order to use this extension, is the same as any other Sinatra extension:

module Zomg
  # Define our app class, we use modular app for this example
  class App < Sinatra::Base
    # this gives us all the namespaces we defined earlier
    register Routes

    get '/' do
      "Welcome to my application!"
    end
  end
end

Zomg::App.run! # Don't forget to start your app ;)

Phew! That was a mouthful.

I hope that helps you use ‘Sinatra::Namespace` in every way imaginable!

Defined Under Namespace

Modules: BaseMethods, InstanceMethods, NamespacedMethods, SharedMethods

Class Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



368
369
370
# File 'lib/sinatra/namespace.rb', line 368

def self.extended(base)
  base.extend BaseMethods
end

.new(base, pattern, conditions = {}, &block) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/sinatra/namespace.rb', line 189

def self.new(base, pattern, conditions = {}, &block)
  Module.new do
    # quelch uninitialized variable warnings, since these get used by compile method.
    @pattern = nil
    @conditions = nil
    extend NamespacedMethods
    include InstanceMethods
    @base = base
    @extensions = []
    @errors = {}
    @pattern, @conditions = compile(pattern, conditions)
    @templates            = Hash.new { |_h, k| @base.templates[k] }
    namespace = self
    before { extend(@namespace = namespace) }
    class_eval(&block)
  end
end