Class: Aspera::Cli::PluginFactory

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/aspera/cli/plugin_factory.rb

Overview

Instantiate plugin from well-known locations

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePluginFactory

Returns a new instance of PluginFactory.



16
17
18
19
20
# File 'lib/aspera/cli/plugin_factory.rb', line 16

def initialize
  @lookup_folders = []
  # information on plugins
  @plugins = {}
end

Instance Attribute Details

#lookup_foldersObject (readonly)

Returns the value of attribute lookup_folders.



14
15
16
# File 'lib/aspera/cli/plugin_factory.rb', line 14

def lookup_folders
  @lookup_folders
end

Instance Method Details

#add_lookup_folder(folder) ⇒ Object

add a folder to the list of folders to look for plugins



33
34
35
# File 'lib/aspera/cli/plugin_factory.rb', line 33

def add_lookup_folder(folder)
  @lookup_folders.unshift(folder)
end

#add_plugins_from_lookup_foldersObject

find plugins in defined paths



38
39
40
41
42
43
44
45
46
47
# File 'lib/aspera/cli/plugin_factory.rb', line 38

def add_plugins_from_lookup_folders
  @lookup_folders.each do |folder|
    next unless File.directory?(folder)
    # TODO: add gem root to load path ? and require short folder ?
    # $LOAD_PATH.push(folder) if i[:add_path]
    Dir.entries(folder).select{|file|file.end_with?(RUBY_FILE_EXT)}.each do |source|
      add_plugin_info(File.join(folder, source))
    end
  end
end

#create(plugin_name_sym, **args) ⇒ Object

Create specified plugin

Parameters:

  • plugin_name_sym (Symbol)

    name of plugin

  • args (Hash)

    arguments to pass to plugin constructor



60
61
62
63
# File 'lib/aspera/cli/plugin_factory.rb', line 60

def create(plugin_name_sym, **args)
  # TODO: check that ancestor is Plugin?
  plugin_class(plugin_name_sym).new(**args)
end

#plugin_class(plugin_name_sym) ⇒ Object

Returns Class object for plugin.

Returns:

  • Class object for plugin



50
51
52
53
54
55
# File 'lib/aspera/cli/plugin_factory.rb', line 50

def plugin_class(plugin_name_sym)
  raise "ERROR: plugin not found: #{plugin_name_sym}" unless @plugins.key?(plugin_name_sym)
  require @plugins[plugin_name_sym][:require_stanza]
  # Module.nesting[1] is Aspera::Cli
  return Object.const_get("#{Module.nesting[1]}::#{PLUGINS_MODULE}::#{plugin_name_sym.to_s.capitalize}")
end

#plugin_listObject

Returns list of registered plugins.

Returns:

  • list of registered plugins



23
24
25
# File 'lib/aspera/cli/plugin_factory.rb', line 23

def plugin_list
  @plugins.keys
end

#plugin_source(plugin_name_sym) ⇒ Object

Returns path to source file of plugin.

Returns:

  • path to source file of plugin



28
29
30
# File 'lib/aspera/cli/plugin_factory.rb', line 28

def plugin_source(plugin_name_sym)
  @plugins[plugin_name_sym][:source]
end