Class: Hanami::Slice::Router Private

Inherits:
Router
  • Object
show all
Defined in:
lib/hanami/slice/router.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.

‘Hanami::Router` subclass with enhancements for use within Hanami apps.

This is loaded from Hanami apps and slices and made available as their router.

Since:

  • 2.0.0

Defined Under Namespace

Classes: ResourceBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(routes:, inflector:, middleware_stack: Routing::Middleware::Stack.new, prefix: ::Hanami::Router::DEFAULT_PREFIX, **kwargs, &blk) ⇒ Router

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.

Returns a new instance of Router.

Since:

  • 2.0.0



26
27
28
29
30
31
32
33
# File 'lib/hanami/slice/router.rb', line 26

def initialize(routes:, inflector:, middleware_stack: Routing::Middleware::Stack.new, prefix: ::Hanami::Router::DEFAULT_PREFIX, **kwargs, &blk)
  @path_prefix = Hanami::Router::Prefix.new(prefix)
  @inflector = inflector
  @middleware_stack = middleware_stack
  @resource_scope = []
  instance_eval(&blk)
  super(**kwargs, &routes)
end

Instance Attribute Details

#inflectorObject (readonly)

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:

  • 2.0.0



17
18
19
# File 'lib/hanami/slice/router.rb', line 17

def inflector
  @inflector
end

#middleware_stackObject (readonly)

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:

  • 2.0.0



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

def middleware_stack
  @middleware_stack
end

#path_prefixObject (readonly)

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:

  • 2.0.0



23
24
25
# File 'lib/hanami/slice/router.rb', line 23

def path_prefix
  @path_prefix
end

Instance Method Details

#freezeObject

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:

  • 2.0.0



36
37
38
39
40
41
# File 'lib/hanami/slice/router.rb', line 36

def freeze
  return self if frozen?

  remove_instance_variable(:@middleware_stack)
  super
end

#resource(name, **options, &block) ⇒ Object

Generates RESTful routes for a singular resource.

Examples:

resource :profile
# Generates (singular, no index):
# GET    /profile/new    profile.new
# POST   /profile        profile.create
# GET    /profile        profile.show
# GET    /profile/edit   profile.edit
# PATCH  /profile        profile.update
# DELETE /profile        profile.destroy

Parameters:

  • name (Symbol)

    the resource name (singular)

  • options (Hash)

    options for customizing the routes

Options Hash (**options):

  • :only (Array<Symbol>)

    limit to specific actions

  • :except (Array<Symbol>)

    exclude specific actions

  • :to (String)

    the action key namespace, e.g. “namespace.action”

  • :path (String)

    the URL path

  • :as (String, Symbol)

    the route name prefix

Since:

  • 2.3.0



134
135
136
# File 'lib/hanami/slice/router.rb', line 134

def resource(name, **options, &block)
  build_resource(name, :singular, options, &block)
end

#resources(name, **options, &block) ⇒ Object

Generates RESTful routes for a plural resource.

Examples:

resources :users
# Generates:
# GET    /users          users.index
# GET    /users/new      users.new
# POST   /users          users.create
# GET    /users/:id      users.show
# GET    /users/:id/edit users.edit
# PATCH  /users/:id      users.update
# DELETE /users/:id      users.destroy

Parameters:

  • name (Symbol)

    the resource name (plural)

  • options (Hash)

    options for customizing the routes

Options Hash (**options):

  • :only (Array<Symbol>)

    Limit to specific actions

  • :except (Array<Symbol>)

    Exclude specific actions

  • :to (String)

    the action key namespace, e.g. “namespace.action”

  • :path (String)

    the URL path

  • :as (String, Symbol)

    the route name prefix

Since:

  • 2.3.0



108
109
110
# File 'lib/hanami/slice/router.rb', line 108

def resources(name, **options, &block)
  build_resource(name, :plural, options, &block)
end

#slice(slice_name, at:, as: nil, &blk) ⇒ Object

Yields a block for routes to resolve their action components from the given slice.

An optional URL prefix may be supplied with ‘at:`.

Examples:

# config/routes.rb

module MyApp
  class Routes < Hanami::Routes
    slice :admin, at: "/admin" do
      # Will route to the "actions.posts.index" component in Admin::Slice
      get "posts", to: "posts.index"
    end
  end
end

Parameters:

  • slice_name (Symbol)

    the slice’s name

  • at (String, nil)

    optional URL prefix for the routes

Since:

  • 2.0.0



74
75
76
77
78
79
80
81
82
83
# File 'lib/hanami/slice/router.rb', line 74

def slice(slice_name, at:, as: nil, &blk)
  blk ||= @resolver.find_slice(slice_name).routes

  prev_resolver = @resolver
  @resolver = @resolver.to_slice(slice_name)

  scope(at, as:, &blk)
ensure
  @resolver = prev_resolver
end

#to_rack_appObject

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:

  • 2.0.0



44
45
46
# File 'lib/hanami/slice/router.rb', line 44

def to_rack_app
  middleware_stack.to_rack_app(self)
end

#use(*args, **kwargs, &blk) ⇒ Object

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:

  • 2.0.0



49
50
51
# File 'lib/hanami/slice/router.rb', line 49

def use(*args, **kwargs, &blk)
  middleware_stack.use(*args, **kwargs.merge(path_prefix: path_prefix.to_s), &blk)
end