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.



183
184
185
186
# File 'lib/roda/plugins/hash_routes.rb', line 183

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.



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/roda/plugins/hash_routes.rb', line 191

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.



216
217
218
219
# File 'lib/roda/plugins/hash_routes.rb', line 216

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.



209
210
211
# File 'lib/roda/plugins/hash_routes.rb', line 209

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.



226
227
228
229
230
231
232
233
# File 'lib/roda/plugins/hash_routes.rb', line 226

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.



238
239
240
241
242
# File 'lib/roda/plugins/hash_routes.rb', line 238

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