Module: Roda::RodaPlugins::MultiRun::ClassMethods

Defined in:
lib/roda/plugins/multi_run.rb

Instance Method Summary collapse

Instance Method Details

#freezeObject

Convert app blocks into apps by calling them, in order to force autoloads and to speed up subsequent calls. Freeze the multi_run apps so that there can be no thread safety issues at runtime.



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/roda/plugins/multi_run.rb', line 76

def freeze
  app_blocks = opts[:multi_run_app_blocks]
  apps = opts[:multi_run_apps]
  app_blocks.each do |prefix, block|
    apps[prefix] = block.call
  end
  app_blocks.clear.freeze
  apps.freeze
  self::RodaRequest.refresh_multi_run_regexp!
  super
end

#multi_run_appsObject

Hash storing rack applications to dispatch to, keyed by the prefix for the application.



90
91
92
# File 'lib/roda/plugins/multi_run.rb', line 90

def multi_run_apps
  opts[:multi_run_apps]
end

#run(prefix, app = nil, &block) ⇒ Object

Add a rack application to dispatch to for the given prefix when r.multi_run is called. If a block is given, it is called every time there is a request for the route to get the app to call. If neither a block or an app is provided, any stored route for the prefix is removed. It is an error to provide both an app and block in the same call.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/roda/plugins/multi_run.rb', line 99

def run(prefix, app=nil, &block)
  prefix = prefix.to_s
  if app 
    raise Roda::RodaError, "cannot provide both app and block to Roda.run" if block
    opts[:multi_run_apps][prefix] = app
    opts[:multi_run_app_blocks].delete(prefix)
  elsif block
    opts[:multi_run_apps].delete(prefix)
    opts[:multi_run_app_blocks][prefix] = block
  else
    opts[:multi_run_apps].delete(prefix)
    opts[:multi_run_app_blocks].delete(prefix)
  end
  self::RodaRequest.refresh_multi_run_regexp!
end