Class: Speedflow::Plugin::Manager

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 Attribute Summary collapse

Instance Method Summary collapse

Methods included from Message

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

Constructor Details

#initialize(plugins = []) ⇒ Manager

Public: Constructor

plugins - Array of plugins

Examples

Manager.new([])
# => <Speedflow::Plugin::Manager>

Returns an Arrays of plugins.



28
29
30
31
# File 'lib/speedflow/plugin/manager.rb', line 28

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

Instance Attribute Details

#configObject

Public: Plugin configuration object.

Returns Speedflow::Plugin::Configuration.



110
111
112
# File 'lib/speedflow/plugin/manager.rb', line 110

def config
  @config ||= Configuration
end

#promptObject

Public: Plugin prompt object.

Returns Speedflow::Plugin::Prompt.



117
118
119
# File 'lib/speedflow/plugin/manager.rb', line 117

def prompt
  @prompt ||= ::TTY::Prompt
end

Instance Method Details

#action(plugin, action_name) ⇒ Object

Call action.

plugin_name - Plugin name. action_name - Action name. arguments - List of arguments.

Returns Hash of action output.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/speedflow/plugin/manager.rb', line 79

def action(plugin, action_name)
  plugin_name = plugin.class.name.split('::')[-2].downcase
  action_name = action_name.prepend('action_').underscore

  unless plugin.respond_to?(action_name)
    message = "Unable to call action: #{plugin_name}.#{action_name}"
    raise PluginActionNotFound, message
  end

  # TODO: Remove underground prompt.
  success "Run action '#{action_name}' of '#{plugin_name}' plugin."

  plugin.send action_name
end

#action_from_step(step) ⇒ Object

Call action from flow step.

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

Returns nothing.



56
57
58
59
# File 'lib/speedflow/plugin/manager.rb', line 56

def action_from_step(step)
  plugin = plugin(step['plugin'], step['arguments'])
  action(plugin, step['action'])
end

#load_pluginsObject

Public: Load plugins

Returns nothing.



36
37
38
# File 'lib/speedflow/plugin/manager.rb', line 36

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

#plugin(plugin_name, arguments) ⇒ Object

Call plugin.

plugin_name - Plugin name. arguments - List of arguments.

Returns <Speedflow::Plugin::Abstract>.



67
68
69
70
# File 'lib/speedflow/plugin/manager.rb', line 67

def plugin(plugin_name, arguments)
  configuration = config.new(arguments, plugin_name)
  plugin_object(plugin_name).new(configuration, prompt.new)
end

#plugin_object(plugin_name) ⇒ Object

Public: Get plugin object from plugin name

plugin_name - Plugin name.

TODO: Create Utils::name_to_object method.

Returns Object.



101
102
103
104
105
# File 'lib/speedflow/plugin/manager.rb', line 101

def plugin_object(plugin_name)
  plugin_name = plugin_name.downcase.capitalize

  "Speedflow::Plugin::#{plugin_name}::Plugin".constantize
end

#require_plugin(plugin) ⇒ Object

Public: Require a plugins (from Gem)

Returns nothing.



43
44
45
46
47
48
49
# File 'lib/speedflow/plugin/manager.rb', line 43

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