Class: TTY::Plugins
Overview
A class responsible for managing plugins installation
Instance Attribute Summary collapse
-
#plugins ⇒ Object
readonly
Returns the value of attribute plugins.
Instance Method Summary collapse
-
#activate ⇒ Object
Activate all registered plugins that are not already enabled and add lib paths to $LOAD_PATH.
-
#each(&block) ⇒ self
Iterate over plugins.
-
#initialize ⇒ Plugins
constructor
Initialize the Plugins.
-
#load_from(gemspec_path, pattern) ⇒ Object
Loads gemspec from a file and registers gems matching pattern.
-
#loaded?(name) ⇒ Boolean
private
Check if plugin is already loaded.
-
#names ⇒ Object
Return a list of all plugin names as strings.
-
#register(name, plugin = false) ⇒ Object
Register plugin with name in internal array.
- #size ⇒ Object (also: #length)
- #to_a ⇒ Object
- #to_ary ⇒ Object
Constructor Details
#initialize ⇒ Plugins
Initialize the Plugins
15 16 17 |
# File 'lib/tty/plugins.rb', line 15 def initialize @plugins = [] end |
Instance Attribute Details
#plugins ⇒ Object (readonly)
Returns the value of attribute plugins.
10 11 12 |
# File 'lib/tty/plugins.rb', line 10 def plugins @plugins end |
Instance Method Details
#activate ⇒ Object
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
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.
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
90 91 92 |
# File 'lib/tty/plugins.rb', line 90 def loaded?(name) plugins.any? { |plugin| plugin.name == name } end |
#names ⇒ Object
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
39 40 41 42 |
# File 'lib/tty/plugins.rb', line 39 def register(name, plugin = false) return unless plugin && !loaded?(name) @plugins << plugin end |
#size ⇒ Object Also known as: length
102 103 104 |
# File 'lib/tty/plugins.rb', line 102 def size to_ary.size end |
#to_a ⇒ Object
98 99 100 |
# File 'lib/tty/plugins.rb', line 98 def to_a to_ary end |
#to_ary ⇒ Object
94 95 96 |
# File 'lib/tty/plugins.rb', line 94 def to_ary @plugins.dup end |