class Webgen::BundleLoader
This class is used for loading extension bundles. It provides a DSL for the most commonly needed commands.
When an extension bundle is provided by a Rubygem and the Rubygem is not already activated, the Rubygem is automatically activated. This only works when one follows the standard naming conventions for webgen extension bundles, i.e. the Rubygem has to be named 'webgen-BUNDLE_NAME-bundle'.
Public Class Methods
Create a new BundleLoader object belonging
to the website object website.
# File lib/webgen/bundle_loader.rb, line 82 def initialize(website, ext_dir) @website = website @website.ext.bundle_infos = BundleInformation.new @ext_dir = ext_dir @loaded = [] @stack = [] end
Public Instance Methods
Load the extension bundle in the context of this BundleLoader object.
# File lib/webgen/bundle_loader.rb, line 91 def load(name) file = resolve_init_file(name) raise Webgen::BundleLoadError.new(name) if !file file = File.expand_path(file) return if @loaded.include?(file) load!(file) if file != File.expand_path(File.join(@ext_dir, 'init.rb')) name = File.basename(File.dirname(file)) info_file = File.join(File.dirname(file), 'info.yaml') @website.ext.bundle_infos.add_bundle(name, File.file?(info_file) ? info_file : nil) end end
Force-loads the given file and does just that (i.e. no checking if file exists and no bundle registration).
*Note*: This method should normally not be called in an extension bundle, use the load method instead.
# File lib/webgen/bundle_loader.rb, line 111 def load!(file) @loaded.push(file) @stack.push(file) self.instance_eval(File.read(file), file) @stack.pop end
Loads all bundles that are marked for auto-loading.
# File lib/webgen/bundle_loader.rb, line 119 def load_autoload_bundles bundles = Gem::Specification.map {|s| s.name }.uniq.map do |gem_name| md = /^webgen-(.*)-bundle$/.match(gem_name) next unless md md[1] end.compact bundles += $LOAD_PATH.map do |path| Dir[File.join(path, 'webgen/bundle', '*')].map {|d| File.basename(d)} end.flatten.compact bundles.each do |bundle_name| file = resolve_init_file(bundle_name) next unless file info_file = File.join(File.dirname(file), 'info.yaml') next unless File.file?(info_file) next unless (begin YAML.load(File.read(info_file))['autoload']; rescue Exception; false end) load(bundle_name) end end
Private Instance Methods
Create all possible initialization file names for the given directory name.
# File lib/webgen/bundle_loader.rb, line 161 def possible_init_file_names(dir_name) [File.join(@ext_dir, dir_name, 'init.rb')] + $LOAD_PATH.map {|path| File.join(path, 'webgen/bundle', dir_name, 'init.rb')} end
Search in the website extension directory and then in the load path to find the initialization file of the bundle.
# File lib/webgen/bundle_loader.rb, line 144 def resolve_init_file(name) name.sub!(/(\/|^)init\.rb$/, '') if name =~ /\A[\w-]+\z/ begin Gem::Specification.find_by_name("webgen-#{name}-bundle").activate rescue Gem::LoadError end end possible_init_file_names(name).each {|path| return path if File.file?(path)} nil end
DSL methods
↑ topPublic Instance Methods
Return the absolute path of the given path which is assumed to be relative to the currently loaded file.
# File lib/webgen/bundle_loader.rb, line 199 def absolute_path(path) File.expand_path(File.join(File.dirname(@stack.last), path)) end
Mount the directory relative to the currently loaded file on the given mount point as passive source.
See Webgen::Source for more information.
# File lib/webgen/bundle_loader.rb, line 193 def mount_passive(dir, mount_point = '/', glob = '{*,**/*}') @website.ext.source.passive_sources.unshift([mount_point, :file_system, absolute_path(dir), glob]) end
Define a configuration option.
See Webgen::Configuration#define_option for more information.
# File lib/webgen/bundle_loader.rb, line 180 def option(name, default, &validator) @website.config.define_option(name, default, &validator) end
Require the file relative to the currently loaded file.
# File lib/webgen/bundle_loader.rb, line 173 def require_relative(file) require(File.join(File.dirname(@stack.last), file)) end
Return the website object.
# File lib/webgen/bundle_loader.rb, line 185 def website @website end