module Roda::RodaPlugins::AutoloadHashBranches::ClassMethods

  1. lib/roda/plugins/autoload_hash_branches.rb

Public Instance methods

autoload_hash_branch(namespace='', segment, file)

Autoload the given file when there is request for the hash branch. The given file should configure the hash branch specified.

[show source]
   # File lib/roda/plugins/autoload_hash_branches.rb
43 def autoload_hash_branch(namespace='', segment, file)
44   segment = "/#{segment}"
45   file = File.expand_path(file)
46   opts[:autoload_hash_branch_files] << file
47   routes = opts[:hash_branches][namespace] ||= {}
48   meth = routes[segment] = define_roda_method(routes[segment] || "hash_branch_#{namespace}_#{segment}", 1) do |r|
49     loc = method(routes[segment]).source_location
50     require file
51     # Avoid infinite loop in case method is not overridden
52     if method(meth).source_location != loc
53       send(meth, r)
54     end
55   end
56   nil
57 end
autoload_hash_branch_dir(namespace='', dir)

For each .rb file in the given directory, add an autoloaded hash branch based on the file name.

[show source]
   # File lib/roda/plugins/autoload_hash_branches.rb
61 def autoload_hash_branch_dir(namespace='', dir)
62   Dir.new(dir).entries.each do |file|
63     if file =~ /\.rb\z/i
64       autoload_hash_branch(namespace, file.sub(/\.rb\z/i, ''), File.join(dir, file))
65     end
66   end
67 end
freeze()

Eagerly load all hash branches when freezing the application.

[show source]
   # File lib/roda/plugins/autoload_hash_branches.rb
70 def freeze
71   opts.delete(:autoload_hash_branch_files).each{|file| require file}
72   super
73 end