Class: TTY::Plugins

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

Overview

A class responsible for managing plugins installation

Constant Summary collapse

PLUGIN_PREFIX =
'tty'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlugins

Initialize the Plugins



14
15
16
# File 'lib/tty/plugins.rb', line 14

def initialize
  @plugins = []
end

Instance Attribute Details

#plugins=(value) ⇒ Object

Sets the attribute plugins

Parameters:

  • value

    the value to set the attribute plugins to.



8
9
10
# File 'lib/tty/plugins.rb', line 8

def plugins=(value)
  @plugins = value
end

Instance Method Details

#findObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find all installed TTY plugins and store them



43
44
45
46
47
48
49
50
51
# File 'lib/tty/plugins.rb', line 43

def find
  Gem.refresh
  Gem::Specification.each do |gem|
    next unless gem.name =~ /^#{PLUGIN_PREFIX}/
    plugin_name = gem.name[/^#{PLUGIN_PREFIX}-(.*)/, 1]
    register(plugin_name, Plugin.new(plugin_name, gem))
  end
  plugins
end

#loadObject

Load all plugins that are not enabled



21
22
23
24
25
# File 'lib/tty/plugins.rb', line 21

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

#namesObject

Return a list of all plugin names as strings



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

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

#register(name, plugin = false) ⇒ Object

Register plugin with name in internal array

Parameters:

  • name (String)
  • plugin (TTY::Plugin) (defaults to: false)


34
35
36
37
38
# File 'lib/tty/plugins.rb', line 34

def register(name, plugin = false)
  if plugin && !loaded?(name)
    plugins << plugin
  end
end