module Roda::RodaPlugins::SharedVars

  1. lib/roda/plugins/shared_vars.rb

The shared_vars plugin adds a shared method for storing shared variables across nested Roda apps.

class API < Roda
  plugin :shared_vars
  route do |r|
    user = shared[:user]
    # ...
  end
end

class App < Roda
  plugin :shared_vars

  route do |r|
    r.on Integer do |user_id|
      shared[:user] = User[user_id]
      r.run API
    end
  end
end

If you pass a hash to shared, it will update the shared vars with the content of the hash:

route do |r|
  r.on Integer do |user_id|
    shared(user: User[user_id])
    r.run API
  end
end

You can also pass a block to shared, which will set the shared variables only for the given block, restoring the previous shared variables afterward:

route do |r|
  r.on Integer do |user_id|
    shared(user: User[user_id]) do
      r.run API
    end
  end
end