Class: Roda::RodaPlugins::HashRoutes::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/roda/plugins/hash_routes.rb

Overview

Internal class handling the internals of the hash_routes class method blocks.

Instance Method Summary collapse

Constructor Details

#initialize(roda, namespace) ⇒ DSL

Returns a new instance of DSL.



275
276
277
278
# File 'lib/roda/plugins/hash_routes.rb', line 275

def initialize(roda, namespace)
  @roda = roda
  @namespace = namespace
end

Instance Method Details

#dispatch_from(namespace = '', branch, &block) ⇒ Object

Setup the given branch in the given namespace to dispatch to routes in this namespace. If a block is given, call the block with the request before dispatching to routes in this namespace.



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/roda/plugins/hash_routes.rb', line 283

def dispatch_from(namespace='', branch, &block)
  ns = @namespace
  if block
    meth_hash = @roda.opts[:hash_routes_methods]
    key = [:dispatch_from, namespace, branch].freeze
    meth = meth_hash[key] = @roda.define_roda_method(meth_hash[key] || "hash_routes_dispatch_from_#{namespace}_#{branch}", 1, &block)
    @roda.hash_branch(namespace, branch) do |r|
      send(meth, r)
      r.hash_routes(ns)
    end
  else
    @roda.hash_branch(namespace, branch) do |r|
      r.hash_routes(ns)
    end
  end
end

#is(path, &block) ⇒ Object

Use the segment to setup a path in the current namespace. If path is given as a string, it is prefixed with a slash. If path is true, the empty string is used as the path.



308
309
310
311
# File 'lib/roda/plugins/hash_routes.rb', line 308

def is(path, &block)
  path = path == true ? "" : "/#{path}"
  @roda.hash_path(@namespace, path, &block)
end

#on(segment, &block) ⇒ Object

Use the segment to setup a branch in the current namespace.



301
302
303
# File 'lib/roda/plugins/hash_routes.rb', line 301

def on(segment, &block)
  @roda.hash_branch(@namespace, segment, &block)
end

#view(path, template) ⇒ Object

Use the segment to setup a path in the current namespace that will render the view with the given name if the GET method is used, and will return a 404 if another request method is used. If path is given as a string, it is prefixed with a slash. If path is true, the empty string is used as the path.



318
319
320
321
322
323
324
325
# File 'lib/roda/plugins/hash_routes.rb', line 318

def view(path, template)
  path = path == true ? "" : "/#{path}"
  @roda.hash_path(@namespace, path) do |r|
    r.get do
      view(template)
    end
  end
end

#views(templates) ⇒ Object

For each template in the array of templates, setup a path in the current namespace for the template using the same name as the template.



330
331
332
333
334
# File 'lib/roda/plugins/hash_routes.rb', line 330

def views(templates)
  templates.each do |template|
    view(template, template)
  end
end