Method: Grape::DSL::Routing::ClassMethods#namespace

Defined in:
lib/grape/dsl/routing.rb

#namespace(space = nil, options = {}, &block) ⇒ Object Also known as: group, resource, resources, segment

Declare a “namespace”, which prefixes all subordinate routes with its name. Any endpoints within a namespace, or group, resource, segment, etc., will share their parent context as well as any configuration done in the namespace context.

Examples:


namespace :foo do
  get 'bar' do
    # defines the endpoint: GET /foo/bar
  end
end


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/grape/dsl/routing.rb', line 165

def namespace(space = nil, options = {}, &block)
  @namespace_description = nil unless instance_variable_defined?(:@namespace_description) && @namespace_description

  if space || block_given?
    within_namespace do
      previous_namespace_description = @namespace_description
      @namespace_description = (@namespace_description || {}).deep_merge(namespace_setting(:description) || {})
      nest(block) do
        if space
          namespace_stackable(:namespace, Namespace.new(space, **options))
        end
      end
      @namespace_description = previous_namespace_description
    end
  else
    Namespace.joined_space_path(namespace_stackable(:namespace))
  end
end