Class: TTY::Plugin

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

Overview

A class responsible for plugin loading

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, gem) ⇒ Plugin

Initialize a Plugin

Parameters:

  • name (String)

    the plugin name

  • gem (Gem::Specification)

    the rubygems gem



23
24
25
26
27
28
# File 'lib/tty/plugins/plugin.rb', line 23

def initialize(name, gem)
  @name     = name
  @gem      = gem
  @gem_name = "tty-#{name}"
  @enabled  = false
end

Instance Attribute Details

#enabledObject

Returns the value of attribute enabled.



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

def enabled
  @enabled
end

#gemObject (readonly)

Returns the value of attribute gem.



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

def gem
  @gem
end

#gem_nameObject (readonly)

Returns the value of attribute gem_name.



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

def gem_name
  @gem_name
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/tty/plugins/plugin.rb', line 6

def name
  @name
end

Instance Method Details

#enabled?Boolean

Check if this plugin has been enabled

Returns:

  • (Boolean)


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

def enabled?
  enabled
end

#load!Object

Load the plugin (require the gem)



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

def load!
  begin
    require gem_name unless enabled?
  rescue LoadError => error
    TTY.shell.error("Unable to load plugin #{gem_name} due to #{error}.")
  rescue => error
    TTY.shell.error("require '#{gem_name}' failed with #{error}")
  end
  @enabled = true
end