Class: Aspera::Cli::Plugins::Factory

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFactory

Returns a new instance of Factory.



17
18
19
20
21
# File 'lib/aspera/cli/plugins/factory.rb', line 17

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

Instance Attribute Details

#lookup_foldersObject (readonly)

Returns the value of attribute lookup_folders.



15
16
17
# File 'lib/aspera/cli/plugins/factory.rb', line 15

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



29
30
31
# File 'lib/aspera/cli/plugins/factory.rb', line 29

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

#add_plugins_from_lookup_foldersObject

find plugins in defined paths



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/aspera/cli/plugins/factory.rb', line 34

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).each do |source|
      next unless source.end_with?(Environment::RB_EXT)
      path = File.join(folder, source)
      plugin_symbol = File.basename(path, Environment::RB_EXT).to_sym
      next if IGNORE_PLUGINS.include?(plugin_symbol)
      req = path.sub(/#{Environment::RB_EXT}$/o, '')
      Aspera.assert(!@plugins.key?(plugin_symbol), type: :warn){"Plugin already registered: #{plugin_symbol}"}
      @plugins[plugin_symbol] = {source: path, require_stanza: req}
    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



68
69
70
71
# File 'lib/aspera/cli/plugins/factory.rb', line 68

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



58
59
60
61
62
63
# File 'lib/aspera/cli/plugins/factory.rb', line 58

def plugin_class(plugin_name_sym)
  Aspera.assert(@plugins.key?(plugin_name_sym), type: NoSuchElement){"plugin not found: #{plugin_name_sym}"}
  require @plugins[plugin_name_sym][:require_stanza]
  # Module.nesting[1] is Aspera::Cli::Plugins
  return Object.const_get("#{Module.nesting[1]}::#{plugin_name_sym.to_s.snake_to_capital}")
end

#plugin_listObject

Returns list of registered plugins.

Returns:

  • list of registered plugins



24
25
26
# File 'lib/aspera/cli/plugins/factory.rb', line 24

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



52
53
54
55
# File 'lib/aspera/cli/plugins/factory.rb', line 52

def plugin_source(plugin_name_sym)
  Aspera.assert(@plugins.key?(plugin_name_sym), type: NoSuchElement){"plugin not found: #{plugin_name_sym}"}
  @plugins[plugin_name_sym][:source]
end