Class: Flexo::Plugin

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

Overview

Base class of all plugins. All plugins wanting to be loaded by Flexo must subclass this class or face the wrath of Flexo::PluginManager.

If the plugin requires persistent storage, keeping it as a YAML-file like ~/.flexo/<plugin>.yaml would make Flexo::Config load it when starting, and making them available to the plugin through a simple call to @manager.config. But that requires plugins to make their own namespace, preferably something like plugins.<pluginname>.key to prevent collisions.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugins, *args) ⇒ Plugin

Variable, mutex, check.



20
21
22
23
24
25
26
# File 'lib/flexo/plugin.rb', line 20

def initialize(plugins, *args)
  @manager  = Flexo::Manager.instance
  @plugins  = plugins
  @triggers = []
  @handlers = []
  @manager.logger.debug "Created #{self.class}, with #{@plugins.to_a}"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Overriding method_missing to let plugins use commands from other plugins. If the plugin tries to use a function that doesn’t exist, check the list of loaded plugins if any of those respond to the function.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/flexo/plugin.rb', line 51

def method_missing(method_name, *args, &block)
  @manager.logger.debug "Method #{method_name} not found in #{self.class}"
  @plugins.each do |plugin_name, plugin|
    @manager.logger.debug "  Checking in #{plugin_name}"
    if plugin.respond_to?(method_name)
      @manager.logger.debug "    Found #{method_name} in #{plugin_name}"
      return plugin.__send__(method_name, *args, &block)
    end
  end

  @manager.logger.debug "  Method #{method_name} not found in any loaded plugins"
  super
end

Class Method Details

.const_missing(const) ⇒ Object



15
16
17
# File 'lib/flexo/plugin.rb', line 15

def self.const_missing(const)
  super unless [:NAME, :VERSION, :SUMMARY, :DEPENDS].include?(const)
end

Instance Method Details

#add_trigger(pattern, &block) ⇒ Object

Convenience. See Flexo::Dispatcher.add_trigger



92
93
94
95
# File 'lib/flexo/plugin.rb', line 92

def add_trigger(pattern, &block)
  @manager.logger.debug("Adding a trigger against #{pattern}")
  mutex { @triggers << @manager.dispatcher.add_trigger(pattern, &block) }
end

#has_method?(method) ⇒ Boolean

Searches loaded plugins for a method. Used by plugins to see if certain optional functionality is available, instead of adding the plugin providing the function as a requirement.

Returns:

  • (Boolean)


68
69
70
71
72
73
74
# File 'lib/flexo/plugin.rb', line 68

def has_method?(method)
  @plugins.each do |name, plugin|
    return true if plugin.respond_to?(method)
  end

  return false
end

#mutexObject

Synchronize mutex



77
78
79
# File 'lib/flexo/plugin.rb', line 77

def mutex
  @manager.pluginmutex { yield }
end

#remove_trigger(trigger) ⇒ Object

Convenience. See Flexo::Dispatcher.remove_trigger



98
99
100
# File 'lib/flexo/plugin.rb', line 98

def remove_trigger(trigger)
  @manager.dispatcher.remove_trigger(trigger)
end

#subscribe(event, &block) ⇒ Object

Convenience. See Flexo::Dispatcher.subscribe



82
83
84
# File 'lib/flexo/plugin.rb', line 82

def subscribe(event, &block)
  mutex { @handlers << @manager.dispatcher.subscribe(event, &block) }
end

#unloadObject

Unloading a plugin. This default action unsubscribes all triggers and handlers, so nothing goes wrong when the plugin is unloaded and the dispatcher still tries to run their blocks



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/flexo/plugin.rb', line 31

def unload
  @manager.logger.debug "Plugin: Unloading"
  @manager.logger.debug "  has #{@triggers.length} triggers"
  @manager.logger.debug "  and #{@handlers.length} handlers"
  
  @triggers.each do |trigger|
    @manager.logger.debug "  Removing trigger #{trigger}"
    remove_trigger trigger
  end
  
  @handlers.each do |handler|
    @manager.logger.debug "  Removing handler #{handler}"
    unsubscribe handler
  end
end

#unsubscribe(handler) ⇒ Object

Convenience. See Flexo::Dispatcher.unsubscribe



87
88
89
# File 'lib/flexo/plugin.rb', line 87

def unsubscribe(handler)
  @manager.dispatcher.unsubscribe(handler)
end