Module: Doing::Plugins

Defined in:
lib/doing/plugin_manager.rb

Overview

Plugin handling

Class Method Summary collapse

Class Method Details

.available_plugins(type: :export) ⇒ Array<String>

Return array of available plugin names

Parameters:

  • type (defaults to: :export)

    Plugin type (:import, :export)

Returns:



138
139
140
141
# File 'lib/doing/plugin_manager.rb', line 138

def available_plugins(type: :export)
  type = valid_type(type)
  plugins[type].keys.sort
end

.list_plugins(options = {}) ⇒ Object

List available plugins to stdout

Parameters:

  • options (type, separator) (defaults to: {})


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/doing/plugin_manager.rb', line 114

def list_plugins(options = {})
  separator = options[:column] ? "\n" : "\t"
  type = options[:type].nil? || options[:type] =~ /all/i ? 'all' : valid_type(options[:type])

  case type
  when :import
    puts plugin_names(type: :import, separator: separator)
  when :export
    puts plugin_names(type: :export, separator: separator)
  else
    print 'Import plugins: '
    puts plugin_names(type: :import, separator: ', ')
    print 'Export plugins: '
    puts plugin_names(type: :export, separator: ', ')
  end
end

.load_plugins(add_dir = nil) ⇒ Object

Load plugins from plugins folder



21
22
23
24
25
26
27
28
29
# File 'lib/doing/plugin_manager.rb', line 21

def load_plugins(add_dir = nil)
  plugins_path(add_dir).each do |plugin_search_path|
    Dir.glob(File.join(plugin_search_path, '**', '*.rb')).sort.each do |plugin|
      require plugin
    end
  end

  plugins
end

.plugin_names(type: :export, separator: '|') ⇒ String

Return string version of plugin names

Parameters:

  • type (defaults to: :export)

    Plugin type (:import, :export)

  • separator (defaults to: '|')

    The separator to join names with

Returns:



151
152
153
154
# File 'lib/doing/plugin_manager.rb', line 151

def plugin_names(type: :export, separator: '|')
  type = valid_type(type)
  available_plugins(type: type).join(separator)
end

.plugin_regex(type: :export) ⇒ Object

Return a regular expression of all plugin triggers for type

Parameters:

  • type (defaults to: :export)

    The type :import or :export



162
163
164
165
166
167
168
169
# File 'lib/doing/plugin_manager.rb', line 162

def plugin_regex(type: :export)
  type = valid_type(type)
  pattern = []
  plugins[type].each do |_, options|
    pattern << options[:trigger].normalize_trigger
  end
  Regexp.new("^(?:#{pattern.join('|')})$", true)
end

.plugin_templates(type: :export) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/doing/plugin_manager.rb', line 171

def plugin_templates(type: :export)
  type = valid_type(type)
  templates = []
  plugs = plugins[type].clone
  plugs.delete_if { |_t, o| o[:templates].nil? }.each do |_, options|
    options[:templates].each do |t|
      templates << t[:name]
    end
  end

  templates
end

.pluginsObject



11
12
13
14
15
16
# File 'lib/doing/plugin_manager.rb', line 11

def plugins
  @plugins ||= {
    import: {},
    export: {}
  }
end

.plugins_path(add_dir = nil) ⇒ Object

Public: Setup the plugin search path

Returns an Array of plugin search paths



34
35
36
37
38
# File 'lib/doing/plugin_manager.rb', line 34

def plugins_path(add_dir = nil)
  paths = Array(File.join(File.dirname(__FILE__), 'plugins'))
  paths << File.join(add_dir) if add_dir
  paths.map { |d| File.expand_path(d) }
end

.register(title, type, klass) ⇒ Object

Register a plugin

param: +[String|Array]+ title The name of the plugin (can be an array of names)

param: +type+ The plugin type (:import, :export)

param: +klass+ The class responding to :render or :import

returns: Success boolean



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/doing/plugin_manager.rb', line 52

def register(title, type, klass)
  type = validate_plugin(title, type, klass)
  return unless type

  if title.is_a?(Array)
    title.each { |t| register(t, type, klass) }
    return
  end

  settings = if klass.respond_to? :settings
               klass.settings
             else
               { trigger: title.normalize_trigger, config: {} }
             end

  plugins[type] ||= {}
  plugins[type][title] = {
    trigger: settings[:trigger].normalize_trigger || title.normalize_trigger,
    class: klass,
    templates: settings[:templates] || nil,
    config: settings[:config] || {}
  }

  return unless ENV['DOING_PLUGIN_DEBUG']

  Doing.logger.debug('Plugin Manager:', "Registered #{type} plugin \"#{title}\"")
end

.template_for_trigger(trigger, type: :export) ⇒ Object



196
197
198
199
200
201
202
203
204
205
# File 'lib/doing/plugin_manager.rb', line 196

def template_for_trigger(trigger, type: :export)
  type = valid_type(type)
  plugs = plugins[type].clone
  plugs.delete_if { |_t, o| o[:templates].nil? }.each do |_, options|
    options[:templates].each do |t|
      return options[:class].template(trigger) if trigger =~ /^(?:#{t[:trigger].normalize_trigger})$/
    end
  end
  raise Errors::InvalidArgument, "No template type matched \"#{trigger}\""
end

.template_regex(type: :export) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
# File 'lib/doing/plugin_manager.rb', line 184

def template_regex(type: :export)
  type = valid_type(type)
  pattern = []
  plugs = plugins[type].clone
  plugs.delete_if { |_t, o| o[:templates].nil? }.each do |_, options|
    options[:templates].each do |t|
      pattern << t[:trigger].normalize_trigger
    end
  end
  Regexp.new("^(?:#{pattern.join('|')})$", true)
end

.user_homeObject



7
8
9
# File 'lib/doing/plugin_manager.rb', line 7

def user_home
  @user_home ||= Util.user_home
end

.valid_type(type, default: nil) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/doing/plugin_manager.rb', line 93

def valid_type(type, default: nil)
  type ||= default

  t = type.to_s
  type = case t
         when /^i(m(p(o(r(t)?)?)?)?)?$/
           :import
         when /^e(x(p(o(r(t)?)?)?)?)?$/
           :export
         else
           raise Errors::InvalidPluginType, 'Invalid plugin type'
         end

  type.to_sym
end

.validate_plugin(title, type, klass) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/doing/plugin_manager.rb', line 80

def validate_plugin(title, type, klass)
  type = valid_type(type)
  if type == :import && !klass.respond_to?(:import)
    raise Errors::PluginUncallable.new('Import plugins must respond to :import', type: type, plugin: title)
  end

  if type == :export && !klass.respond_to?(:render)
    raise Errors::PluginUncallable.new('Export plugins must respond to :render', type: type, plugin: title)
  end

  type
end