Class: Hanami::Slice::Router Private
- 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.
Defined Under Namespace
Classes: ResourceBuilder
Instance Attribute Summary collapse
- #inflector ⇒ Object readonly private
- #middleware_stack ⇒ Object readonly private
- #path_prefix ⇒ Object readonly private
Instance Method Summary collapse
- #freeze ⇒ Object private
-
#initialize(routes:, inflector:, middleware_stack: Routing::Middleware::Stack.new, prefix: ::Hanami::Router::DEFAULT_PREFIX, **kwargs, &blk) ⇒ Router
constructor
private
A new instance of Router.
-
#resource(name, **options, &block) ⇒ Object
Generates RESTful routes for a singular resource.
-
#resources(name, **options, &block) ⇒ Object
Generates RESTful routes for a plural resource.
-
#slice(slice_name, at:, as: nil, &blk) ⇒ Object
Yields a block for routes to resolve their action components from the given slice.
- #to_rack_app ⇒ Object private
- #use(*args, **kwargs, &blk) ⇒ Object private
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.
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
#inflector ⇒ Object (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.
17 18 19 |
# File 'lib/hanami/slice/router.rb', line 17 def inflector @inflector end |
#middleware_stack ⇒ Object (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.
20 21 22 |
# File 'lib/hanami/slice/router.rb', line 20 def middleware_stack @middleware_stack end |
#path_prefix ⇒ Object (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.
23 24 25 |
# File 'lib/hanami/slice/router.rb', line 23 def path_prefix @path_prefix end |
Instance Method Details
#freeze ⇒ 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.
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.
134 135 136 |
# File 'lib/hanami/slice/router.rb', line 134 def resource(name, **, &block) build_resource(name, :singular, , &block) end |
#resources(name, **options, &block) ⇒ Object
Generates RESTful routes for a plural resource.
108 109 110 |
# File 'lib/hanami/slice/router.rb', line 108 def resources(name, **, &block) build_resource(name, :plural, , &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:`.
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_app ⇒ 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.
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.
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 |