Module: Roda::RodaPlugins::AutoloadHashBranches::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#autoload_hash_branch(namespace = '', segment, file) ⇒ Object

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



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

def autoload_hash_branch(namespace='', segment, file)
  segment = "/#{segment}"
  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

#autoload_hash_branch_dir(namespace = '', dir) ⇒ Object

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



55
56
57
58
59
60
61
# File 'lib/roda/plugins/autoload_hash_branches.rb', line 55

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