Class: Webgen::BundleLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/webgen/bundle_loader.rb

Overview

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

Defined Under Namespace

Classes: BundleInformation

Instance Method Summary collapse

Constructor Details

#initialize(website, ext_dir) ⇒ BundleLoader

Create a new BundleLoader object belonging to the website object website.



83
84
85
86
87
88
89
# File 'lib/webgen/bundle_loader.rb', line 83

def initialize(website, ext_dir)
  @website = website
  @website.ext.bundle_infos = BundleInformation.new
  @ext_dir = ext_dir
  @loaded = []
  @stack = []
end

Instance Method Details

#absolute_path(path) ⇒ Object

Return the absolute path of the given path which is assumed to be relative to the currently loaded file.



200
201
202
# File 'lib/webgen/bundle_loader.rb', line 200

def absolute_path(path)
  File.expand_path(File.join(File.dirname(@stack.last), path))
end

#load(name) ⇒ Object

Load the extension bundle in the context of this BundleLoader object.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/webgen/bundle_loader.rb', line 92

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

#load!(file) ⇒ Object

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.



112
113
114
115
116
117
# File 'lib/webgen/bundle_loader.rb', line 112

def load!(file)
  @loaded.push(file)
  @stack.push(file)
  self.instance_eval(File.read(file), file)
  @stack.pop
end

#load_autoload_bundlesObject

Loads all bundles that are marked for auto-loading.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/webgen/bundle_loader.rb', line 120

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

#mount_passive(dir, mount_point = '/', glob = '{*,**/*}') ⇒ Object

Mount the directory relative to the currently loaded file on the given mount point as passive source.

See Webgen::Source for more information.



194
195
196
# File 'lib/webgen/bundle_loader.rb', line 194

def mount_passive(dir, mount_point = '/', glob = '{*,**/*}')
  @website.ext.source.passive_sources.unshift([mount_point, :file_system, absolute_path(dir), glob])
end

#option(name, default, &validator) ⇒ Object

Define a configuration option.

See Webgen::Configuration#define_option for more information.



181
182
183
# File 'lib/webgen/bundle_loader.rb', line 181

def option(name, default, &validator)
  @website.config.define_option(name, default, &validator)
end

#require_relative(file) ⇒ Object

Require the file relative to the currently loaded file.



174
175
176
# File 'lib/webgen/bundle_loader.rb', line 174

def require_relative(file)
  require(File.join(File.dirname(@stack.last), file))
end

#websiteObject

Return the website object.



186
187
188
# File 'lib/webgen/bundle_loader.rb', line 186

def website
  @website
end