Module: Roda::RodaPlugins::AutoloadNamedRoutes::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#autoload_named_route(namespace = nil, name, file) ⇒ Object

Autoload the given file when there is request for the named route. The given file should configure the named route specified.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/roda/plugins/autoload_named_routes.rb', line 40

def autoload_named_route(namespace=nil, name, file)
  file = File.expand_path(file)
  opts[:autoload_named_route_files] << file
  routes = opts[:namespaced_routes][namespace] ||= {}
  meth = routes[name] = define_roda_method(routes[name] || "named_routes_#{namespace}_#{name}", 1) do |r|
    loc = method(routes[name]).source_location
    require file
    # Avoid infinite loop in case method is not overridden
    if method(meth).source_location != loc
      send(meth, r)
    end
  end
  nil
end

#freezeObject

Eagerly load all autoloaded named routes when freezing the application.



56
57
58
59
# File 'lib/roda/plugins/autoload_named_routes.rb', line 56

def freeze
  opts.delete(:autoload_named_route_files).each{|file| require file} unless opts.frozen?
  super
end