class Vmpooler::Providers

Public Class Methods

installed_providers() click to toggle source

@return [Array] - returns an array of gem names that contain a provider

# File lib/vmpooler/providers.rb, line 24
def self.installed_providers
  new.vmpooler_provider_gem_list.map(&:name)
end
load_all_providers() click to toggle source

@return [Array] - array of provider files ie. [“lib/vmpooler/providers/base.rb”, “lib/vmpooler/providers/dummy.rb”, “lib/vmpooler/providers/vsphere.rb”] although these files can come from any gem

# File lib/vmpooler/providers.rb, line 19
def self.load_all_providers
  new.load_from_gems
end
load_by_name(names) click to toggle source

@param names [Array] - an array of names or string name of a provider @return [Array] - list of provider files loaded ie. [“lib/vmpooler/providers/base.rb”, “lib/vmpooler/providers/dummy.rb”, “lib/vmpooler/providers/vsphere.rb”]

# File lib/vmpooler/providers.rb, line 10
def self.load_by_name(names)
  names = Array(names)
  instance = new
  names.map { |name| instance.load_from_gems(name) }.flatten
end

Public Instance Methods

load_from_gems(name = nil) click to toggle source

Internal: Find any gems containing vmpooler provider plugins and load the main file in them.

@return [Array] - a array of provider files @param name [String] - the name of the provider to load

# File lib/vmpooler/providers.rb, line 37
def load_from_gems(name = nil)
  paths = gem_directories.map do |gem_path|
    # we don't exactly know if the provider name matches the main file name that should be loaded
    # so we use globs to get everything like the name
    # this could mean that vsphere5 and vsphere6 are loaded when only vsphere5 is used
    Dir.glob(File.join(gem_path, "*#{name}*.rb")).sort.each do |file|
      require file
    end
  end
  paths.flatten
end
vmpooler_provider_gem_list() click to toggle source

@return [Array] returns a list of vmpooler providers gem plugin specs

# File lib/vmpooler/providers.rb, line 29
def vmpooler_provider_gem_list
  gemspecs.find_all { |spec| File.directory?(File.join(spec.full_gem_path, provider_path)) } + included_lib_dirs
end

Private Instance Methods

excluded_classes() click to toggle source

Add constants to array to skip over classes, ie. Vmpooler::PoolManager::Provider::Dummy

# File lib/vmpooler/providers.rb, line 58
def excluded_classes
  []
end
gem_directories() click to toggle source

Internal: Retrieve a list of available gem paths from RubyGems.

Returns an Array of Pathname objects.

# File lib/vmpooler/providers.rb, line 88
def gem_directories
  dirs = []
  if rubygems?
    dirs = gemspecs.map do |spec|
      lib_path = File.expand_path(File.join(spec.full_gem_path, provider_path))
      lib_path if File.exist? lib_path
    end + included_lib_dirs
  end
  dirs.reject(&:nil?).uniq
end
gemspecs() click to toggle source

Internal: Retrieve a list of available gemspecs.

Returns an Array of Gem::Specification objects.

# File lib/vmpooler/providers.rb, line 109
def gemspecs
  @gemspecs ||= if Gem::Specification.respond_to?(:latest_specs)
                  Gem::Specification.latest_specs
                else
                  Gem.searcher.init_gemspecs
                end
end
included_lib_dirs() click to toggle source

paths to include in the search path

# File lib/vmpooler/providers.rb, line 63
def included_lib_dirs
  []
end
plugin_classes() click to toggle source

returns an array of plugin classes by looking in the object space for all loaded classes that start with Vmpooler::PoolManager::Provider

# File lib/vmpooler/providers.rb, line 69
def plugin_classes
  unless @plugin_classes
    load_plugins
    # weed out any subclasses in the formatter
    klasses = ObjectSpace.each_object(Class).find_all do |c|
      c.name && c.name.split('::').count == 3 && c.name =~ /Vmpooler::PoolManager::Provider/
    end
    @plugin_classes = klasses - excluded_classes || []
  end
  @plugin_classes
end
plugin_map() click to toggle source
# File lib/vmpooler/providers.rb, line 81
def plugin_map
  @plugin_map ||= Hash[plugin_classes.map { |gem| [gem.send(:name), gem] }]
end
provider_path() click to toggle source

@return [String] - the relative path to the vmpooler provider dir this is used when searching gems for this path

# File lib/vmpooler/providers.rb, line 53
def provider_path
  File.join('lib', 'vmpooler', 'providers')
end
rubygems?() click to toggle source

Internal: Check if RubyGems is loaded and available.

Returns true if RubyGems is available, false if not.

# File lib/vmpooler/providers.rb, line 102
def rubygems?
  defined? ::Gem
end