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.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/roda/plugins/autoload_hash_branches.rb', line 43

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

#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.



61
62
63
64
65
66
67
# File 'lib/roda/plugins/autoload_hash_branches.rb', line 61

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

#freezeObject

Eagerly load all hash branches when freezing the application.



70
71
72
73
# File 'lib/roda/plugins/autoload_hash_branches.rb', line 70

def freeze
  opts.delete(:autoload_hash_branch_files).each{|file| require file}
  super
end