Class: Hanami::Routes Private

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

Overview

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

Routes factory

A Hanami 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) ⇒ Hanami::Routes

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.

Initialize the factory

Parameters:

  • routes (Hanami::Router)

    a routes set

Since:

  • 0.1.0



20
21
22
# File 'lib/hanami/routes.rb', line 20

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



209
210
211
212
213
214
215
216
217
# File 'lib/hanami/routes.rb', line 209

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) ⇒ Hanami::Utils::Escape::SafeString

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.

Return a relative path for the given route name

Examples:

Basic example

require 'hanami'

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

Web.routes.path(:login)
  # => '/login'

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

Dynamic finders

require 'hanami'

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

Web.routes.
  # => '/login'

Web.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:

  • (Hanami::Utils::Escape::SafeString)

    the corresponding relative URL

Raises:

  • Hanami::Routing::InvalidRouteException

See Also:

Since:

  • 0.1.0



75
76
77
# File 'lib/hanami/routes.rb', line 75

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

#recognize(env) ⇒ Hanami::Routing::RecognizedRoute

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.

Recognize a route from a Rack env.

This method is designed for testing purposes

Examples:

Path Generation

# spec/web/routes_spec.rb
RSpec.describe Web::Routes do
  it 'generates "/"' do
    actual = described_class.path(:root)
    expect(actual).to eq '/'
  end

  it 'generates "/books/23"' do
    actual = described_class.path(:book, id: 23)
    expect(actual).to eq '/books/23'
  end
end

Route Recognition

# spec/web/routes_spec.rb
RSpec.describe Web::Routes do

  # ...

  it 'recognizes "GET /"' do
    env   = Rack::MockRequest.env_for('/')
    route = described_class.recognize(env)

    expect(route).to be_routable

    expect(route.path).to   eq '/'
    expect(route.verb).to   eq 'GET'
    expect(route.params).to eq({})
  end

  it 'recognizes "PATCH /books/23"' do
    env   = Rack::MockRequest.env_for('/books/23', method: 'PATCH')
    route = described_class.recognize(env)

    expect(route).to be_routable

    expect(route.path).to   eq '/books/23'
    expect(route.verb).to   eq 'PATCH'
    expect(route.params).to eq(id: '23')
  end

  it 'does not recognize unknown route' do
    env   = Rack::MockRequest.env_for('/foo')
    route = described_class.recognize(env)

    expect(route).to_not be_routable
  end
end

Parameters:

  • env (Hash)

    a Rack env

Returns:

  • (Hanami::Routing::RecognizedRoute)

    the recognized route

See Also:

Since:

  • 0.8.0



201
202
203
# File 'lib/hanami/routes.rb', line 201

def recognize(env)
  @routes.recognize(env)
end

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

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.

Return an absolute path for the given route name

Examples:

Basic example

require 'hanami'

module Web
  class Application < Hanami::Application
    configure do
      routes do
        scheme 'https'
        host   'bookshelf.org'

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

Web.routes.url(:login)
  # => 'https://bookshelf.org/login'

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

Dynamic finders

require 'hanami'

module Web
  class Application < Hanami::Application
    configure do
      routes do
        scheme 'https'
        host   'bookshelf.org'

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

Web.routes.
  # => 'https://bookshelf.org/login'

Web.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:

  • (Hanami::Utils::Escape::SafeString)

    the corresponding absolute URL

Raises:

  • Hanami::Routing::InvalidRouteException

See Also:

Since:

  • 0.1.0



136
137
138
# File 'lib/hanami/routes.rb', line 136

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