Class: Lotus::Routes

Inherits:
Object
  • Object
show all
Defined in:
lib/lotus/routes.rb

Overview

Routes factory

A Lotus application has this factory instantiated by default and associated to the ‘Routes` constant, under the application namespace.

Since:

  • 0.1.0

Instance Method Summary collapse

Constructor Details

#initialize(routes) ⇒ Lotus::Routes

Initialize the factory

Parameters:

  • routes (Lotus::Router)

    a routes set

Since:

  • 0.1.0



18
19
20
# File 'lib/lotus/routes.rb', line 18

def initialize(routes)
  @routes = routes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.3.0



141
142
143
144
145
146
147
148
149
# File 'lib/lotus/routes.rb', line 141

def method_missing(m, *args)
  named_route, type = m.to_s.split(/\_(path|url)\z/)

  if type
    public_send(type, named_route.to_sym, *args)
  else
    super
  end
end

Instance Method Details

#path(name, *args) ⇒ Lotus::Utils::Escape::SafeString

Return a relative path for the given route name

Examples:

Basic example

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      routes do
        get '/login', to: 'sessions#new', as: :login
      end
    end
  end
end

Bookshelf::Routes.path(:login)
  # => '/login'

Bookshelf::Routes.path(:login, return_to: '/dashboard')
  # => '/login?return_to=%2Fdashboard'

Dynamic finders

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      routes do
        get '/login', to: 'sessions#new', as: :login
      end
    end
  end
end

Bookshelf::Routes.
  # => '/login'

Bookshelf::Routes.(return_to: '/dashboard')
  # => '/login?return_to=%2Fdashboard'

Parameters:

  • name (Symbol)

    the route name

  • args (Array, nil)

    an optional set of arguments that is passed down to the wrapped route set.

Returns:

  • (Lotus::Utils::Escape::SafeString)

    the corresponding relative URL

Raises:

  • Lotus::Routing::InvalidRouteException

See Also:

Since:

  • 0.1.0



73
74
75
# File 'lib/lotus/routes.rb', line 73

def path(name, *args)
  Utils::Escape::SafeString.new(@routes.path(name, *args))
end

#url(name, *args) ⇒ Lotus::Utils::Escape::SafeString

Return an absolute path for the given route name

Examples:

Basic example

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      routes do
        scheme 'https'
        host   'bookshelf.org'

        get '/login', to: 'sessions#new', as: :login
      end
    end
  end
end

Bookshelf::Routes.url(:login)
  # => 'https://bookshelf.org/login'

Bookshelf::Routes.url(:login, return_to: '/dashboard')
  # => 'https://bookshelf.org/login?return_to=%2Fdashboard'

Dynamic finders

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      routes do
        scheme 'https'
        host   'bookshelf.org'

        get '/login', to: 'sessions#new', as: :login
      end
    end
  end
end

Bookshelf::Routes.
  # => 'https://bookshelf.org/login'

Bookshelf::Routes.(return_to: '/dashboard')
  # => 'https://bookshelf.org/login?return_to=%2Fdashboard'

Parameters:

  • name (Symbol)

    the route name

  • args (Array, nil)

    an optional set of arguments that is passed down to the wrapped route set.

Returns:

  • (Lotus::Utils::Escape::SafeString)

    the corresponding absolute URL

Raises:

  • Lotus::Routing::InvalidRouteException

See Also:

Since:

  • 0.1.0



134
135
136
# File 'lib/lotus/routes.rb', line 134

def url(name, *args)
  Utils::Escape::SafeString.new(@routes.url(name, *args))
end