class ACLS::Loader

Public Class Methods

auto(paths, opts={}) click to toggle source

Use one or more paths to autoload a set of Ruby source files.

# File lib/acls/loader.rb, line 5
def auto(paths, opts={})
  opts = default_opts.merge(opts)
  autoload_trees(build_trees(paths, opts), opts)
end
default_opts() click to toggle source
# File lib/acls/loader.rb, line 10
def default_opts
  {root_ns: false, exclude: [], immediate: []}
end

Private Class Methods

autoload_statement(tree) click to toggle source
# File lib/acls/loader.rb, line 21
def autoload_statement(tree)
  "autoload :#{tree.name}, \"#{tree.source}\""
end
autoload_tree(mod, tree, opts) click to toggle source
# File lib/acls/loader.rb, line 25
def autoload_tree(mod, tree, opts)
  unless exclude?(tree, opts)
    if tree.children.empty?
      mod.module_eval(autoload_statement(tree))
    else
      mod = submodule(mod, tree.name)
    end
    tree.children.map { |child| autoload_tree(mod, child, opts) }
  end
end
autoload_trees(trees, opts) click to toggle source
# File lib/acls/loader.rb, line 36
def autoload_trees(trees, opts)
  trees.map do |tree|
    root = Object
    tree.children.map do |node|
      autoload_tree(root, node, opts)
    end
  end
end
build_trees(paths, opts) click to toggle source
# File lib/acls/loader.rb, line 16
def build_trees(paths, opts)
  paths = [paths] unless paths.respond_to?(:map)
  paths.map { |path| Parser.parse(path) }
end
exclude?(tree, opts) click to toggle source
# File lib/acls/loader.rb, line 45
def exclude?(tree, opts)
  opts[:exclude].each do |pattern|
    if pattern.is_a?(String)
      return true if pattern == File.basename(tree.path, ".rb")
    else
      return true if pattern.match(tree.path)
    end
  end
  false
end
submodule(parent, child) click to toggle source
# File lib/acls/loader.rb, line 56
def submodule(parent, child)
  parent.const_get(child, false)
rescue NameError
  parent.const_set(child, Module.new)
end