Class: Speedflow::PluginManager

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Inflector, Message
Defined in:
lib/speedflow/plugin_manager.rb

Overview

Used to manage the plugins

Constant Summary collapse

PLUGIN_BASE =
'speedflow-plugin-'.freeze

Instance Method Summary collapse

Methods included from Message

#error, #info, #message, #notice, #success

Constructor Details

#initialize(plugins = []) ⇒ PluginManager

Public: Constructor

plugins - Array of plugins

Examples

PluginManager.new({})
# => <Speedflow::PluginManager>

Returns an Arrays of plugins.



19
20
21
22
# File 'lib/speedflow/plugin_manager.rb', line 19

def initialize(plugins = [])
  @plugins = plugins || []
  load_plugins
end

Instance Method Details

#call_action_from_step(step) ⇒ Object

Call action from flow step.

step - Hash from flow. ”, action: ”, arguments: ”.

Returns nothing.



47
48
49
50
51
# File 'lib/speedflow/plugin_manager.rb', line 47

def call_action_from_step(step)
  step['arguments'] ||= {}
  success "Run action '#{step['action']}' of '#{step['plugin']}' plugin."
  call_plugin_action(step['plugin'], step['action'], step['arguments'])
end

#call_plugin_action(plugin, action, arguments) ⇒ Object

Call plugin action.

plugin - Plugin name. action - Action name. arguments - List of arguments

Returns nothing.



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/speedflow/plugin_manager.rb', line 60

def call_plugin_action(plugin, action, arguments)
  action = underscore(action.prepend('action_'))
  module_name = plugin.downcase.capitalize.prepend('Speedflow::Plugin::')
  begin
    Kernel.const_get(module_name).send(action.to_s, arguments)
  rescue NoMethodError => exception
    message = "Unable to call action: #{module_name}.#{action}\n"
    message << exception.message
    raise PluginActionNotFound, message
  end
end

#load_pluginsObject

Public: Load plugins

Returns nothing.



27
28
29
# File 'lib/speedflow/plugin_manager.rb', line 27

def load_plugins
  @plugins.each { |plugin| require_plugin(plugin) }
end

#require_plugin(plugin) ⇒ Object

Public: Require a plugins (from Gem)

Returns nothing.



34
35
36
37
38
39
40
# File 'lib/speedflow/plugin_manager.rb', line 34

def require_plugin(plugin)
  require plugin.downcase.prepend(PLUGIN_BASE)
rescue LoadError
  message = "Unable to load plugin '#{plugin}'.\n"
  message << "Help: `gem install #{PLUGIN_BASE}#{plugin}`"
  raise PluginNotFound, message
end