Class: Pangrid::Plugin

Inherits:
Object
  • Object
show all
Includes:
PluginUtils
Defined in:
lib/pangrid/plugin.rb

Constant Summary collapse

REGISTRY =
{}
FAILED =
[]
MISSING_DEPS =
{}
DESCRIPTION =
""

Class Method Summary collapse

Methods included from PluginUtils

#check

Class Method Details

.class_to_name(str) ⇒ Object

utility functions



99
100
101
102
103
104
# File 'lib/pangrid/plugin.rb', line 99

def self.class_to_name(str)
  str.gsub(/.*:/, '').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1-\2').
    gsub(/([a-z\d])([A-Z])/,'\1-\2').
    downcase
end

.get(name) ⇒ Object



94
95
96
# File 'lib/pangrid/plugin.rb', line 94

def self.get(name)
  REGISTRY[name]
end

.inherited(subclass) ⇒ Object



40
41
42
43
44
# File 'lib/pangrid/plugin.rb', line 40

def self.inherited(subclass)
  name = class_to_name(subclass.name)
  #puts "Registered #{subclass} as #{name}"
  REGISTRY[name] = subclass
end

.list_allObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/pangrid/plugin.rb', line 65

def self.list_all
  puts "-------------------------------------------------------"
  puts "Available plugins (F = from, T = to):"
  puts "-------------------------------------------------------"
  REGISTRY.keys.sort.each do |name|
    plugin = REGISTRY[name]
    provides = [:read, :write].select {|m| plugin.method_defined? m}
    provides = provides.map {|m| {read: 'F', write: 'T'}[m]}.join
    puts "  #{name} [#{provides}]".ljust(30) + plugin.const_get(:DESCRIPTION)
  end
  if !MISSING_DEPS.empty?
    puts
    puts "-------------------------------------------------------"
    puts "Missing dependencies for plugins:"
    puts "-------------------------------------------------------"
    MISSING_DEPS.keys.sort.each do |name|
      puts "  " + name + ": gem install " + MISSING_DEPS[name].join(" ")
    end
  end
  if !FAILED.empty?
    puts
    puts "The following plugins could not load due to errors:"
    puts "-------------------------------------------------------"
    FAILED.each do |error|
      puts "  " + error
    end
  end
end

.load_allObject



46
47
48
49
50
51
52
53
# File 'lib/pangrid/plugin.rb', line 46

def self.load_all
  REGISTRY.clear
  FAILED.clear
  plugins = Dir.glob(File.dirname(__FILE__) + "/plugins/*.rb")
  plugins.each do |f|
    load_plugin f
  end
end

.load_plugin(filename) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/pangrid/plugin.rb', line 55

def self.load_plugin(filename)
  begin
    require filename
  rescue PluginDependencyError => e
    MISSING_DEPS[e.name] = e.gems
  rescue StandardError => e
    FAILED << "#{File.basename(filename)}: #{e}"
  end
end