Class: Vedeu::Plugins

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

Overview

A class responsible for managing plugins installation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVedeu::Plugins

Returns a new instance of Vedeu::Plugins.



12
13
14
# File 'lib/vedeu/plugins/plugins.rb', line 12

def initialize
  @plugins = []
end

Instance Attribute Details

#inputArray<String> (readonly, protected)

Returns:

  • (Array<String>)


67
# File 'lib/vedeu/plugins/plugins.rb', line 67

attr_accessor :plugins

#pluginsArray<String> (protected)

Returns:

  • (Array<String>)


67
68
69
# File 'lib/vedeu/plugins/plugins.rb', line 67

def plugins
  @plugins
end

Instance Method Details

#findArray<void>

Find all installed plugins and store them.

Returns:

  • (Array<void>)


38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vedeu/plugins/plugins.rb', line 38

def find
  Gem.refresh

  Gem::Specification.each do |gem|
    next unless gem.name =~ /^#{prefix}/

    plugin_name = gem.name[/^#{prefix}(.*)/, 1]

    register(plugin_name, Vedeu::Plugin.new(plugin_name, gem))
  end

  plugins
end

#loadArray<void>

Loads all plugins that are not enabled.

Returns:

  • (Array<void>)


19
20
21
# File 'lib/vedeu/plugins/plugins.rb', line 19

def load
  plugins.each { |plugin| plugin.load! unless plugin.enabled? }
end

#namesHash

Return a list of all plugin names as strings.

Returns:

  • (Hash)


55
56
57
58
59
60
61
# File 'lib/vedeu/plugins/plugins.rb', line 55

def names
  collection = {}
  plugins.each_with_object(collection) do |hash, plugin|
    hash[plugin.name] = plugin
    hash
  end
end

#not_loaded?(name) ⇒ Boolean (private)

Returns a boolean indicating whether a plugin is already loaded.

Parameters:

  • name (NilClass|Symbol|String)

    The name of the model or target model to act upon. May default to ‘Vedeu.focus`.

Returns:



75
76
77
# File 'lib/vedeu/plugins/plugins.rb', line 75

def not_loaded?(name)
  plugins.empty? || plugins.any? { |plugin| plugin.gem_name != name }
end

#prefixString (private)

Returns:

  • (String)


80
81
82
# File 'lib/vedeu/plugins/plugins.rb', line 80

def prefix
  'vedeu_'
end

#register(name, plugin = false) ⇒ Array<void>

Register plugin with name in an internal array.

Parameters:

  • name (NilClass|Symbol|String)

    The name of the model or target model to act upon. May default to ‘Vedeu.focus`.

  • plugin (Vedeu::Plugin) (defaults to: false)

Returns:

  • (Array<void>)


28
29
30
31
32
33
# File 'lib/vedeu/plugins/plugins.rb', line 28

def register(name, plugin = false)
  Vedeu.log(type:    :debug,
            message: "Attempting to register plugin: #{name}")

  plugins << plugin if plugin && not_loaded?(name)
end