Class: Aspera::Cli::Plugins::Factory
- Inherits:
-
Object
- Object
- Aspera::Cli::Plugins::Factory
- Includes:
- Singleton
- Defined in:
- lib/aspera/cli/plugins/factory.rb
Instance Attribute Summary collapse
-
#lookup_folders ⇒ Object
readonly
Returns the value of attribute lookup_folders.
Class Method Summary collapse
-
.instance ⇒ Factory
Returns the singleton instance of Factory.
Instance Method Summary collapse
-
#add_lookup_folder(folder) ⇒ Object
add a folder to the list of folders to look for plugins.
-
#add_plugins_from_lookup_folders ⇒ Object
find plugins in defined paths.
-
#create(plugin_name_sym, **args) ⇒ Object
Create specified plugin.
-
#initialize ⇒ Factory
constructor
A new instance of Factory.
-
#plugin_class(plugin_name_sym) ⇒ Object
Class object for plugin.
-
#plugin_list ⇒ Array<Symbol>
Sorted list of registered plugins.
-
#plugin_source(plugin_name_sym) ⇒ Object
Path to source file of plugin.
Constructor Details
#initialize ⇒ Factory
Returns a new instance of Factory.
20 21 22 23 24 |
# File 'lib/aspera/cli/plugins/factory.rb', line 20 def initialize @lookup_folders = [] # information on plugins @plugins = {} end |
Instance Attribute Details
#lookup_folders ⇒ Object (readonly)
Returns the value of attribute lookup_folders.
18 19 20 |
# File 'lib/aspera/cli/plugins/factory.rb', line 18 def lookup_folders @lookup_folders end |
Class Method Details
.instance ⇒ Factory
Returns the singleton instance of Factory
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/aspera/cli/plugins/factory.rb', line 15 class Factory include Singleton attr_reader :lookup_folders def initialize @lookup_folders = [] # information on plugins @plugins = {} end # @return [Array<Symbol>] Sorted list of registered plugins def plugin_list @plugins.keys.sort end # add a folder to the list of folders to look for plugins def add_lookup_folder(folder) @lookup_folders.unshift(folder) end # find plugins in defined paths 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 # @return path to source file of plugin 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 # @return Class object for plugin 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 # Create specified plugin # @param plugin_name_sym [Symbol] name of plugin # @param args [Hash] arguments to pass to plugin constructor def create(plugin_name_sym, **args) # TODO: check that ancestor is Plugin? plugin_class(plugin_name_sym).new(**args) end IGNORE_PLUGINS = %i[factory base basic_auth oauth] private_constant :IGNORE_PLUGINS end |
Instance Method Details
#add_lookup_folder(folder) ⇒ Object
add a folder to the list of folders to look for plugins
32 33 34 |
# File 'lib/aspera/cli/plugins/factory.rb', line 32 def add_lookup_folder(folder) @lookup_folders.unshift(folder) end |
#add_plugins_from_lookup_folders ⇒ Object
find plugins in defined paths
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/aspera/cli/plugins/factory.rb', line 37 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
71 72 73 74 |
# File 'lib/aspera/cli/plugins/factory.rb', line 71 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.
61 62 63 64 65 66 |
# File 'lib/aspera/cli/plugins/factory.rb', line 61 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_list ⇒ Array<Symbol>
Returns Sorted list of registered plugins.
27 28 29 |
# File 'lib/aspera/cli/plugins/factory.rb', line 27 def plugin_list @plugins.keys.sort end |
#plugin_source(plugin_name_sym) ⇒ Object
Returns path to source file of plugin.
55 56 57 58 |
# File 'lib/aspera/cli/plugins/factory.rb', line 55 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 |