Class: PluginLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/plugin_loader.rb

Constant Summary collapse

GEM_NAME_PREFIX =
/^ghedsh-/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePluginLoader

Returns a new instance of PluginLoader.



5
6
7
# File 'lib/plugin_loader.rb', line 5

def initialize
  @plugins = []
end

Instance Attribute Details

#pluginsObject (readonly)

Returns the value of attribute plugins.



3
4
5
# File 'lib/plugin_loader.rb', line 3

def plugins
  @plugins
end

Instance Method Details

#find_gemsObject



21
22
23
# File 'lib/plugin_loader.rb', line 21

def find_gems
  gem_list.select { |gem| gem.name =~ GEM_NAME_PREFIX }
end

#find_pluginsObject



13
14
15
16
17
18
19
# File 'lib/plugin_loader.rb', line 13

def find_plugins
  find_gems.map do |gem|
    @plugins << { name: gem.name, path: gem_path(gem.name), plugin_klass: plugin_klass_name(gem.name) }
  end

  @plugins
end

#gem_listObject



39
40
41
42
# File 'lib/plugin_loader.rb', line 39

def gem_list
  Gem.refresh
  Gem::Specification.respond_to?(:each) ? Gem::Specification : Gem.source_index.find_name('')
end

#gem_path(name) ⇒ Object



25
26
27
# File 'lib/plugin_loader.rb', line 25

def gem_path(name)
  name.tr('-', '/')
end

#load_pluginsObject



9
10
11
# File 'lib/plugin_loader.rb', line 9

def load_plugins
  find_plugins
end

#plugin_klass_name(path) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/plugin_loader.rb', line 29

def plugin_klass_name(path)
  # convert gem paths to plugin module.
  # ghedsh/firstplugin --> Ghedsh::Firstplugin
  # ghedsh/another_name --> Ghedsh::AnotherName
  path = gem_path(path)
  path.split('/').collect do |c|
    c.split('_').collect(&:capitalize).join
  end.join('::')
end