Class: TTY::Plugins

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

Overview

A class responsible for managing plugins installation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlugins

Initialize the Plugins



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

def initialize
  @plugins = []
end

Instance Attribute Details

#pluginsObject (readonly)

Returns the value of attribute plugins.



10
11
12
# File 'lib/tty/plugins.rb', line 10

def plugins
  @plugins
end

Instance Method Details

#activateObject

Activate all registered plugins that are not already enabled and add lib paths to $LOAD_PATH.



71
72
73
74
75
# File 'lib/tty/plugins.rb', line 71

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

#each(&block) ⇒ self

Iterate over plugins

Examples:

plugins.each { |plug| ... }

Returns:

  • (self)


26
27
28
29
30
# File 'lib/tty/plugins.rb', line 26

def each(&block)
  return to_enum unless block_given?
  to_ary.each(&block)
  self
end

#load_from(gemspec_path, pattern) ⇒ Object

Loads gemspec from a file and registers gems matching pattern.

Examples:

plugins.load_from('foo.gemspec', /tty-(.*)/)

Parameters:

  • gemspec_path (String|Pathname)

    the path to gemspec

  • pattern (Regex)

    the pattern to match gems by



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/tty/plugins.rb', line 55

def load_from(gemspec_path, pattern)
  Gem.refresh
  spec = Gem::Specification.load(gemspec_path)
  dependencies = spec.runtime_dependencies.concat(spec.development_dependencies)
  dependencies.each do |gem|
    gem_name = gem.name[pattern]
    next if gem_name.to_s.empty?
    register(gem_name, Plugin.new(gem_name, gem))
  end
  self
end

#loaded?(name) ⇒ Boolean

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.

Check if plugin is already loaded

Returns:

  • (Boolean)


90
91
92
# File 'lib/tty/plugins.rb', line 90

def loaded?(name)
  plugins.any? { |plugin| plugin.name == name }
end

#namesObject

Return a list of all plugin names as strings



80
81
82
83
84
85
# File 'lib/tty/plugins.rb', line 80

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)


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

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

#sizeObject Also known as: length



102
103
104
# File 'lib/tty/plugins.rb', line 102

def size
  to_ary.size
end

#to_aObject



98
99
100
# File 'lib/tty/plugins.rb', line 98

def to_a
  to_ary
end

#to_aryObject



94
95
96
# File 'lib/tty/plugins.rb', line 94

def to_ary
  @plugins.dup
end