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



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

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.



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

def enabled
  @enabled
end

#gemObject (readonly)

Returns the value of attribute gem.



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

def gem
  @gem
end

#gem_nameObject (readonly)

Returns the value of attribute gem_name.



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

def gem_name
  @gem_name
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#enabled?Boolean

Check if this plugin has been enabled

Returns:

  • (Boolean)


37
38
39
# File 'lib/tty/plugins/plugin.rb', line 37

def enabled?
  !!enabled
end

#load!Object

Load the plugin (require the gem)



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

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