Module: LittlePlugger::ClassMethods

Defined in:
lib/bel/vendor/little-plugger.rb

Instance Method Summary collapse

Instance Method Details

#disregard_plugin(*names) ⇒ Object Also known as: disregard_plugins

Add the names to the list of plugins that will not be loaded. This list prevents the plugin system from loading unwanted or unneeded plugins.

If a plugin name appears in both the ‘disregard_plugin’ list and the ‘plugin’ list, the disregard list takes precedence; that is, the plugin will not be loaded.



137
138
139
140
141
# File 'lib/bel/vendor/little-plugger.rb', line 137

def disregard_plugin( *names )
  @disregard_plugin ||= []
  @disregard_plugin.concat(names.map! {|n| n.to_sym})
  @disregard_plugin
end

#initialize_pluginsObject

Iterate over the loaded plugin classes and modules and call the initialize method for each plugin. The plugin’s initialize method is defeind as initialize_plugin_name, where the plugin name is unique to each plugin.



175
176
177
178
179
180
# File 'lib/bel/vendor/little-plugger.rb', line 175

def initialize_plugins
  plugins.each do |name, klass|
    msg = "initialize_#{name}"
    klass.send msg if klass.respond_to? msg
  end
end

#load_pluginsObject

Iterate through all installed gems looking for those that have the plugin_path in their “lib” folder, and load all .rb files found in the gem’s plugin path. Each .rb file should define one class or module that will be used as a plugin.



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/bel/vendor/little-plugger.rb', line 187

def load_plugins
  @loaded ||= {}
  found = {}

  Gem.find_files(File.join(plugin_path, '*.rb')).sort!.reverse_each do |path|
    name = File.basename(path, '.rb').to_sym
    found[name] = path unless found.key? name
  end

  :keep_on_truckin while found.map { |name, path|
    next unless plugin_names.empty? or plugin_names.include? name
    next if disregard_plugins.include? name
    next if @loaded[name]
    begin
      @loaded[name] = load path
    rescue ScriptError, StandardError => err
      warn "Error loading #{path.inspect}: #{err.message}. skipping..."
    end
  }.any?
end

#plugin(*names) ⇒ Object

Add the names to the list of plugins that will be loaded.



125
126
127
# File 'lib/bel/vendor/little-plugger.rb', line 125

def plugin( *names )
  plugin_names.concat(names.map! {|n| n.to_sym})
end

#plugin_moduleObject

This module or class where plugins are located.



216
217
218
# File 'lib/bel/vendor/little-plugger.rb', line 216

def plugin_module
  ::LittlePlugger.default_plugin_module(plugin_path)
end

#plugin_namesObject

Returns the array of plugin names that will be loaded. If the array is empty, then any plugin found in the plugin_path will be loaded.



147
148
149
# File 'lib/bel/vendor/little-plugger.rb', line 147

def plugin_names
  @plugin_names ||= []
end

#plugin_pathObject

The path to search in a gem’s ‘lib’ folder for plugins.



210
211
212
# File 'lib/bel/vendor/little-plugger.rb', line 210

def plugin_path
  ::LittlePlugger.default_plugin_path(self)
end

#pluginsObject

Loads the desired plugins and returns a hash. The hash contains all the plugin classes and modules keyed by the plugin name.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/bel/vendor/little-plugger.rb', line 154

def plugins
  load_plugins
  pm = plugin_module
  names = pm.constants.map { |s| s.to_s }
  names.reject! { |n| n =~ %r/^[A-Z_]+$/ }

  h = {}
  names.each do |name|
    sym = ::LittlePlugger.underscore(name).to_sym
    next unless plugin_names.empty? or plugin_names.include? sym
    next if disregard_plugins.include? sym
    h[sym] = pm.const_get name
  end
  h
end