Class: Ego::Plugin

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

Overview

A plug-in extends Ego with new handlers and other functionality, through a domain-specific language (DSL).

See Also:

Constant Summary collapse

@@plugins =

Manifest of all registered plug-ins

{}
@@context =

Current plug-in context

nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, body, builtin: false) ⇒ Plugin

Returns a new instance of Plugin.

Parameters:

  • name (String)

    the plug-in name

  • body

    the plug-in body

  • builtin (Boolean) (defaults to: false)

    whether this is a built-in plug-in



19
20
21
22
23
# File 'lib/ego/plugin.rb', line 19

def initialize(name, body, builtin: false)
  @name = name
  @body = body
  @builtin = builtin
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



14
15
16
# File 'lib/ego/plugin.rb', line 14

def body
  @body
end

#builtinObject (readonly)

Returns the value of attribute builtin.



14
15
16
# File 'lib/ego/plugin.rb', line 14

def builtin
  @builtin
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/ego/plugin.rb', line 14

def name
  @name
end

Class Method Details

.contextPlugin

Get the currently executing plug-in.

Returns:

  • (Plugin)

    currently executing plugin



64
65
66
# File 'lib/ego/plugin.rb', line 64

def self.context
  @@context
end

.decorate(obj) ⇒ Object

Yield each plug-in body passing obj as a parameter and calling obj#context=.

Parameters:

  • obj (Object)

    the object to decorate

Returns:

  • (Object)

    the decorated object



51
52
53
54
55
56
57
58
59
# File 'lib/ego/plugin.rb', line 51

def self.decorate(obj)
  @@plugins.each_value do |plugin|
    @@context = plugin
    plugin.body.call(obj)
    @@context = nil
  end

  obj
end

.load(paths) ⇒ void

This method returns an undefined value.

Load all given plug-in paths

Parameters:

  • paths (Array)

    absolute paths to plug-in files



29
30
31
# File 'lib/ego/plugin.rb', line 29

def self.load(paths)
  paths.each { |path| Kernel.load path }
end

.register(name, body, builtin: false) ⇒ Plugin

Note:

You should use Ego.plugin in plug-in scripts, which sets a plug-in name for you automatically.

Register a new plug-in

Parameters:

  • name (String)

    the plug-in name

  • body

    the plug-in body

  • builtin (Boolean) (defaults to: false)

    whether to register as a built-in plug-in

Returns:

  • (Plugin)

    the instantiated plug-in



42
43
44
# File 'lib/ego/plugin.rb', line 42

def self.register(name, body, builtin: false)
  @@plugins[name] = Plugin.new(name, body, builtin: builtin)
end