module Nuggets::Pluggable

Attributes

plugin_filename[W]

Public Class Methods

load_plugins_for(*klasses) click to toggle source
# File lib/nuggets/pluggable.rb, line 32
def self.load_plugins_for(*klasses)
  klasses.map { |klass| klass.extend(self).load_plugins }
end

Public Instance Methods

load_plugins(name = plugin_filename) click to toggle source
# File lib/nuggets/pluggable.rb, line 36
def load_plugins(name = plugin_filename)
  load_path_plugins(name)
  load_gem_plugins(name)
  load_env_plugins(name)

  loaded_plugins.to_a
end
loaded_plugins() click to toggle source
# File lib/nuggets/pluggable.rb, line 44
def loaded_plugins
  @loaded_plugins ||= Set.new
end
plugin_filename() click to toggle source
# File lib/nuggets/pluggable.rb, line 48
def plugin_filename
  @plugin_filename ||= "#{name.
    gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
    gsub(/([a-z\d])([A-Z])/,     '\1_\2').
    gsub(/::/, '/').tr('-', '_').downcase}_plugin.rb"
end

Private Instance Methods

load_env_plugins(name) click to toggle source
# File lib/nuggets/pluggable.rb, line 72
def load_env_plugins(name)
  path = ::ENV["#{name.chomp(ext = ::File.extname(name)).upcase}_PATH"]
  path.split(::File::PATH_SEPARATOR).each { |dir|
    load_plugin_files(::Dir["#{dir}/*#{ext}"])
  } if path
end
load_gem_plugins(name) click to toggle source
# File lib/nuggets/pluggable.rb, line 66
def load_gem_plugins(name)
  ::Gem::Specification.latest_specs.each { |spec|
    load_plugin_files(spec.matches_for_glob(name))
  } if ::Object.const_defined?(:Gem)
end
load_path_plugins(name) click to toggle source
# File lib/nuggets/pluggable.rb, line 59
def load_path_plugins(name)
  $LOAD_PATH.dup.each { |path|
    plugin = ::File.expand_path(name, path)
    load_plugin_file(plugin) if ::File.file?(plugin)
  }
end
load_plugin_file(plugin) click to toggle source
# File lib/nuggets/pluggable.rb, line 79
def load_plugin_file(plugin)
  load plugin if loaded_plugins.add?(plugin)
rescue ::Exception => err
  raise unless loaded_plugins.delete?(plugin)
  warn "Error loading #{name} plugin: #{plugin}: #{err} (#{err.class})"
end
load_plugin_files(plugins) click to toggle source
# File lib/nuggets/pluggable.rb, line 86
def load_plugin_files(plugins)
  plugins.each { |plugin| load_plugin_file(::File.expand_path(plugin)) }
end