Class: Olelo::Plugin

Inherits:
Module show all
Includes:
Hooks, Util
Defined in:
lib/olelo/plugin.rb

Overview

Olelo plugin system

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hooks

included, #invoke_hook, #with_hooks

Methods included from Util

#check, #decode64, #deep_copy, #encode64, #escape, #escape_html, #escape_javascript, included, #md5, #no_cache?, #sha256, #titlecase, #truncate, #unescape, #unescape_backslash, #unescape_html, #valid_xml_chars?

Methods inherited from Module

#attr_reader?, #attr_setter, #private_constant, #redefine_method

Constructor Details

#initialize(path, file) ⇒ Plugin

Returns a new instance of Plugin.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/olelo/plugin.rb', line 108

def initialize(path, file)
  @setup = nil
  @path, @file = path, file
  @started = false
  @dependencies = Set.new
  const_set(:PLUGIN, self)

  with_hooks :load do
    names = path.split('/')
    names[0..-2].inject('') do |parent, x|
      parent /= x
      Plugin.load(parent)
      parent
    end

    (0...names.length).inject(Plugin) do |mod, i|
      elem = names[i].split('_').map(&:capitalize).join
      if mod.const_defined?(elem, false)
        mod.const_get(elem)
      else
        child = i == names.length - 1 ? self : Module.new
        child.module_eval { include mod } if mod != Plugin # Include parent module
        mod.const_set(elem, child)
      end
    end

    Plugin.register(path, self)
    module_eval(File.read(file), file)
    Olelo.logger.debug("Plugin #{path} successfully loaded")
  end
end

Class Attribute Details

.dirObject

Returns the value of attribute dir.



15
16
17
# File 'lib/olelo/plugin.rb', line 15

def dir
  @dir
end

.disabledObject

Returns the value of attribute disabled.



15
16
17
# File 'lib/olelo/plugin.rb', line 15

def disabled
  @disabled
end

.failedObject (readonly)

Get failed plugins



18
19
20
# File 'lib/olelo/plugin.rb', line 18

def failed
  @failed
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



104
105
106
# File 'lib/olelo/plugin.rb', line 104

def file
  @file
end

#pathObject (readonly)

Returns the value of attribute path.



104
105
106
# File 'lib/olelo/plugin.rb', line 104

def path
  @path
end

Class Method Details

.enabled?(path) ⇒ Boolean

Check if plugin is enabled

Parameters:

Returns:

  • (Boolean)

    true if enabled



80
81
82
83
84
85
86
87
# File 'lib/olelo/plugin.rb', line 80

def enabled?(path)
  path.split('/').inject('') do |parent, x|
    parent /= x
    return false if disabled.include?(parent)
    parent
  end
  true
end

.for(obj) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/olelo/plugin.rb', line 89

def for(obj)
  if Module === obj
    names = obj.name.split('::')
    mod = Object
    names.map {|name| mod = mod.const_get(name) }.reverse.each do |m|
      return m if Plugin === m
    end
  elsif Proc === obj
    return obj.binding.eval('PLUGIN')
  else
    raise 'Plugin cannot be found for #{obj}'
  end
end

.load(path) ⇒ Boolean

Load plugin by path

Parameters:

  • path (String)

    Plugin path to load

Returns:

  • (Boolean)

    true if every plugin was loaded



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/olelo/plugin.rb', line 39

def load(path)
  path = path.sub(%r{/main$}, '')
  files = [File.join(@dir, path, 'main.rb'), File.join(@dir, "#{path}.rb")].select {|file| File.file?(file) }
  if files.size == 2
    Olelo.logger.error "Duplicate plugin #{files.join(', ')}"
    return false
  end
  return false if files.empty?

  if @loaded.include?(path)
    true
  elsif @failed.include?(path) || !enabled?(path)
    false
  else
    begin
      new(path, files.first)
      true
    rescue Exception => ex
      @failed << path
      if LoadError === ex
        Olelo.logger.warn "Plugin #{path} could not be loaded due to: #{ex.message} (Missing gem?)"
      else
        Olelo.logger.error "Plugin #{path} could not be loaded due to: #{ex.message}"
        Olelo.logger.debug ex
      end
      @loaded.delete(path)
      false
	  end
  end
end

.load_allObject

Load all plugins



71
72
73
# File 'lib/olelo/plugin.rb', line 71

def load_all
  Dir[File.join(@dir, '**', '*.rb')].inject(true) {|result,file| load(file[(@dir.size+1)..-4]) && result }
end

.loadedObject

Get loaded plugins



21
22
23
# File 'lib/olelo/plugin.rb', line 21

def loaded
  @loaded.values
end

.register(path, plugin) ⇒ Object



31
32
33
# File 'lib/olelo/plugin.rb', line 31

def register(path, plugin)
  @loaded[path] = plugin
end

.startvoid

This method returns an undefined value.

Start plugins



27
28
29
# File 'lib/olelo/plugin.rb', line 27

def start
  @loaded.each_value {|plugin| plugin.start }
end

Instance Method Details

#dependencies(*list) ⇒ Object

Load specified plugins and fail with LoadError if dependencies are missing

Parameters:

  • list

    List of plugin paths to load

Returns:

  • List of dependencies (plugin paths)



164
165
166
167
168
169
170
171
172
173
# File 'lib/olelo/plugin.rb', line 164

def dependencies(*list)
  if !list.empty?
    raise 'Plugin is already started' if started?
    @dependencies.merge(list)
    list.each do |dep|
      raise(LoadError, "Could not load dependency #{dep} for #{path}") if !Plugin.load(dep)
    end
  end
  @dependencies
end

#setup(&block) ⇒ Object



175
176
177
# File 'lib/olelo/plugin.rb', line 175

def setup(&block)
  @setup = block
end

#startBoolean

Start the plugin by calling the #setup method

Returns:

  • (Boolean)

    true for success



149
150
151
152
153
154
155
156
157
158
# File 'lib/olelo/plugin.rb', line 149

def start
  return true if @started
  module_eval(&@setup) if @setup
  Olelo.logger.debug "Plugin #{path} successfully started"
  @started = true
rescue Exception => ex
  Olelo.logger.error "Plugin #{path} failed to start due to: #{ex.message}"
  Olelo.logger.error ex
  false
end

#virtual_fsObject

Virtual filesystem used to load plugin assets



141
142
143
144
# File 'lib/olelo/plugin.rb', line 141

def virtual_fs
  VirtualFS::Union.new(VirtualFS::Embedded.new(file),
                       VirtualFS::Native.new(File.dirname(file)))
end