Class: Aspera::Cli::PluginFactory

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

Overview

option is retrieved from another object using accessor

Constant Summary collapse

PLUGINS_MODULE =
'Plugins'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePluginFactory

Returns a new instance of PluginFactory.



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

def initialize
  @lookup_folders = []
  @plugins = {}
end

Instance Attribute Details

#lookup_foldersObject (readonly)

Returns the value of attribute lookup_folders.



9
10
11
# File 'lib/aspera/cli/plugin_factory.rb', line 9

def lookup_folders
  @lookup_folders
end

#pluginsObject (readonly)

Returns the value of attribute plugins.



9
10
11
# File 'lib/aspera/cli/plugin_factory.rb', line 9

def plugins
  @plugins
end

Class Method Details

.plugin_class(plugin_name_sym) ⇒ Object

instantiate a plugin plugins must be Capitalized



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

def plugin_class(plugin_name_sym)
  # Module.nesting[2] is Cli::Plugins
  return Object.const_get("#{Module.nesting[2]}::#{PLUGINS_MODULE}::#{plugin_name_sym.to_s.capitalize}")
end

Instance Method Details

#add_lookup_folder(folder) ⇒ Object



39
40
41
# File 'lib/aspera/cli/plugin_factory.rb', line 39

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

#add_plugin_info(path) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/aspera/cli/plugin_factory.rb', line 28

def add_plugin_info(path)
  raise "ERROR: plugin path must end with #{RUBY_FILE_EXT}" if !path.end_with?(RUBY_FILE_EXT)
  plugin_symbol = File.basename(path, RUBY_FILE_EXT).to_sym
  req = path.sub(/#{RUBY_FILE_EXT}$/o, '')
  if @plugins.key?(plugin_symbol)
    Log.log.warn{"skipping plugin already registered: #{plugin_symbol}"}
    return
  end
  @plugins[plugin_symbol] = {source: path, require_stanza: req}
end

#add_plugins_from_lookup_foldersObject

find plugins in defined paths



44
45
46
47
48
49
50
51
52
53
# File 'lib/aspera/cli/plugin_factory.rb', line 44

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



55
56
57
58
# File 'lib/aspera/cli/plugin_factory.rb', line 55

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