module Roda::RodaPlugins::CustomBlockResults

  1. lib/roda/plugins/custom_block_results.rb

The custom_block_results plugin allows you to specify handling for different block results. By default, Roda only supports nil, false, and string block results, but using this plugin, you can support other block results.

For example, if you wanted to support returning Integer block results, and have them set the response status code, you could do:

plugin :custom_block_results

handle_block_result Integer do |result|
  response.status_code = result
end

route do |r|
  200
end

The expected use case for this is to customize behavior by class, but matching uses ===, so it is possible to use non-class objects that respond to === appropriately.

Note that custom block result handling only occurs if the types are not handled by Roda itself. You cannot use this to modify the handling of nil, false, or string results. Additionally, if the response body has already been written to before the the route block exits, then the result of the block is ignored, and the related handle_block_result block will not be called (this is standard Roda behavior).

The return value of the handle_block_result block is written to the body if the block return value is a String, similar to standard Roda handling of block results. Non-String return values are ignored.

Methods

Public Class

  1. configure

Public Class methods

configure(app)
[show source]
   # File lib/roda/plugins/custom_block_results.rb
42 def self.configure(app)
43   app.opts[:custom_block_results] ||= {}
44 end