Class: DBGeni::Plugin

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlugin

Returns a new instance of Plugin.

[View source]

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

def initialize
end

Class Attribute Details

.hooksObject (readonly)

Returns the value of attribute hooks.


4
5
6
# File 'lib/dbgeni/plugin.rb', line 4

def hooks
  @hooks
end

Class Method Details

.install_plugin(hook, klass) ⇒ Object

[View source]

6
7
8
9
10
11
# File 'lib/dbgeni/plugin.rb', line 6

def install_plugin(hook, klass)
  unless @hooks.has_key? hook
    raise DBGeni::InvalidHook
  end
  @hooks[hook].push klass
end

.resetObject

[View source]

13
14
15
16
17
# File 'lib/dbgeni/plugin.rb', line 13

def reset
  @hooks.keys.each do |k|
    @hooks[k] = Array.new
  end
end

Instance Method Details

#load_plugin(filename) ⇒ Object

[View source]

57
58
59
# File 'lib/dbgeni/plugin.rb', line 57

def load_plugin(filename)
  require filename
end

#load_plugins(path) ⇒ Object

[View source]

46
47
48
49
50
51
52
53
54
55
# File 'lib/dbgeni/plugin.rb', line 46

def load_plugins(path)
  begin
    files = Dir.entries(path).grep(/\.rb$/).sort
  rescue Errno::ENOENT, Errno::EACCES => e
    raise DBGeni::PluginDirectoryNotAccessible, e.to_s
  end
  files.each do |f|
    load_plugin File.join(path, f)
  end
end

#run_plugin(klass, hook, attrs) ⇒ Object

[View source]

72
73
74
75
76
77
78
# File 'lib/dbgeni/plugin.rb', line 72

def run_plugin(klass, hook, attrs)
  instance = klass.new
  unless instance.respond_to? :run
    raise DBGeni::PluginDoesNotRespondToRun
  end
  instance.run(hook, attrs)
end

#run_plugins(hook, attrs) ⇒ Object

[View source]

61
62
63
64
65
66
67
68
69
70
# File 'lib/dbgeni/plugin.rb', line 61

def run_plugins(hook, attrs)
  klasses = self.class.hooks[hook]
  unless klasses.is_a? Array
    raise DBGeni::InvalidHook, hook
  end

  klasses.each do |k|
    run_plugin k, hook, attrs
  end
end