Class: Cartage::Plugin

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

Overview

Cartage::Plugin is the basis of the Cartage plugin system. All plug-ins must inherit from this class.

Direct Known Subclasses

Manifest

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cartage) ⇒ Plugin

A plug-in is initialized with the Cartage instance that owns it.


72
73
74
# File 'lib/cartage/plugin.rb', line 72

def initialize(cartage)
  @cartage = cartage
end

Class Method Details

.commandsObject

These are the command classes provided to the cartage binary. Despite the name being plural, the return can either be a single CmdParse::Command class, or an array of CmdParse::Command class. These command classes should inherit from Cartage::Command, since they will be initialized with a cartage parameter.


67
68
69
# File 'lib/cartage/plugin.rb', line 67

def self.commands
  []
end

.decorate(klass) ⇒ Object

Decorate the provided class with lazy initialization methods.


35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cartage/plugin.rb', line 35

def decorate(klass) #:nodoc:
  registered.each do |plugin|
    name = plugin.name.split(/::/).last.gsub(/([A-Z])/, '_\1').downcase.
      sub(/^_/, '')
    ivar = "@#{name}"

    klass.send(:define_method, name) do
      instance = instance_variable_defined?(ivar) &&
        instance_variable_get(ivar)

      instance ||= instance_variable_set(ivar, plugin.new(self))
    end
  end
end

.inherited(klass) ⇒ Object

Register a subclass.


7
8
9
# File 'lib/cartage/plugin.rb', line 7

def inherited(klass) #:nodoc:
  registered << klass
end

.loadObject

A utility method that will find all Cartage plugins and load them. A Cartage plugin is found with 'cartage/*.rb' and descends from Cartage::Plugin.


19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cartage/plugin.rb', line 19

def load #:nodoc:
  @found ||= {}
  @loaded ||= {}
  @files ||= Gem.find_files('cartage/*.rb')

  @files.reverse.each do |path|
    name = File.basename(path, '.rb').to_sym
    @found[name] = path
  end

  :repeat while @found.map { |name, plugin|
    load_plugin(name, plugin)
  }.any?
end

.registeredObject

The plugins registered with Cartage.


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

def registered
  @registered ||= []
end