module Roda::RodaPlugins::AutoloadHashBranches::ClassMethods

def autoload_hash_branch(namespace='', segment, file)

The given file should configure the hash branch specified.
Autoload the given file when there is request for the hash branch.
def autoload_hash_branch(namespace='', segment, file)
  segment = "/#{segment}"
  file = File.expand_path(file)
  opts[:autoload_hash_branch_files] << file
  routes = opts[:hash_branches][namespace] ||= {}
  meth = routes[segment] = define_roda_method(routes[segment] || "hash_branch_#{namespace}_#{segment}", 1) do |r|
    loc = method(routes[segment]).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

def autoload_hash_branch_dir(namespace='', dir)

based on the file name.
For each .rb file in the given directory, add an autoloaded hash branch
def autoload_hash_branch_dir(namespace='', dir)
  Dir.new(dir).entries.each do |file|
    if file =~ /\.rb\z/i
      autoload_hash_branch(namespace, file.sub(/\.rb\z/i, ''), File.join(dir, file))
    end
  end
end

def freeze

Eagerly load all hash branches when freezing the application.
def freeze
  opts.delete(:autoload_hash_branch_files).each{|file| require file} unless opts.frozen?
  super
end