Module: CodeRay::Plugin

Included in:
Encoders::Encoder, Scanners::Scanner, Styles::Style
Defined in:
lib/coderay/helpers/plugin.rb

Overview

Plugin

Plugins have to include this module.

IMPORTANT: use extend for this module.

Example: see PluginHost.

Instance Method Summary collapse

Instance Method Details

#helper(*helpers) ⇒ Object

Require some helper files.

Example:

class MyPlugin < PluginHost::BaseClass
   register_for :my_id
   helper :my_helper

The above example loads the file myplugin/my_helper.rb relative to the file in which MyPlugin was defined.

You can also load a helper from a different plugin:

helper 'other_plugin/other_helper'


316
317
318
319
320
321
322
323
324
# File 'lib/coderay/helpers/plugin.rb', line 316

def helper *helpers
  for helper in helpers
    if helper.is_a?(String) && helper[/\//]
      self::PLUGIN_HOST.require_helper $`, $'
    else
      self::PLUGIN_HOST.require_helper plugin_id, helper.to_s
    end
  end
end

#included(mod) ⇒ Object



268
269
270
# File 'lib/coderay/helpers/plugin.rb', line 268

def included mod
  warn "#{name} should not be included. Use extend."
end

#plugin_host(host = nil) ⇒ Object

The host for this Plugin class.



293
294
295
296
297
298
299
300
# File 'lib/coderay/helpers/plugin.rb', line 293

def plugin_host host = nil
  if host and not host.is_a? PluginHost
    raise ArgumentError,
      "PluginHost expected, but #{host.class} given."
  end
  self.const_set :PLUGIN_HOST, host if host
  self::PLUGIN_HOST
end

#plugin_idObject

Returns the pulgin id used by the engine.



327
328
329
# File 'lib/coderay/helpers/plugin.rb', line 327

def plugin_id
  name[/[\w_]+$/].downcase
end

#register_for(*ids) ⇒ Object

Register this class for the given langs. Example:

class MyPlugin < PluginHost::BaseClass
  register_for :my_id
  ...
end

See PluginHost.register.



280
281
282
# File 'lib/coderay/helpers/plugin.rb', line 280

def register_for *ids
  plugin_host.register self, *ids
end

#title(title = nil) ⇒ Object



284
285
286
287
288
289
290
# File 'lib/coderay/helpers/plugin.rb', line 284

def title title = nil
  if title
    @title = title.to_s
  else
    @title ||= name[/([^:]+)$/, 1]
  end
end