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



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.



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=.



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



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



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