plugin-loader 0.0.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/plugin/loader.rb +33 -5
- metadata +43 -17
data/lib/plugin/loader.rb
CHANGED
@@ -56,6 +56,7 @@ module Plugin
|
|
56
56
|
@plugin_dir = opts[:plugin_dir] || "#{@data_dir}/plugins"
|
57
57
|
@script_dir = opts[:script_dir] || "#{@data_dir}/scripts"
|
58
58
|
@plugins = {}
|
59
|
+
@etc = {}
|
59
60
|
end
|
60
61
|
#Loads a plugin called 'name' of type 'type'.
|
61
62
|
#It does this by reading the metadata file: "#{@plugin_dir}/name.type".
|
@@ -74,30 +75,44 @@ module Plugin
|
|
74
75
|
#If the format is not available or is not allowed (see: initailize), an exception is raised.
|
75
76
|
#See Plugin::Format for more details on plugin formats.
|
76
77
|
#The '$' key can be any data structure (most commonly a string) and is used by the format declaration to excecute a plugin. See the individual format declarations in plugin/format/*.rb for details of individual formats. If the '$' key is missing, an exception is raised.
|
78
|
+
#
|
79
|
+
#load returns any extra data keys in the JSON file as a hash.
|
77
80
|
def load name, type
|
78
81
|
File.open "#{@plugin_dir}/#{name}.#{type}" do |f|
|
79
82
|
data = JSON.parse(f.read)
|
80
83
|
unless data.has_key? '$'
|
81
|
-
raise "
|
84
|
+
raise "#{type} plugin '#{name}' has an invalid metadata file. It is missing the required '$' key!"
|
82
85
|
end
|
83
86
|
unless data.has_key? 'format'
|
84
|
-
raise "
|
87
|
+
raise "#{type} plugin '#{name}' has an invalid metadata file. It is missing the required 'format' key!"
|
85
88
|
end
|
86
89
|
unless @plugins.has_key? type
|
87
90
|
@plugins[type] = {}
|
88
91
|
end
|
89
|
-
|
92
|
+
unless @etc.has_key? type
|
93
|
+
@etc[type] = {}
|
94
|
+
end
|
95
|
+
fmt = data.delete 'format'
|
96
|
+
format = Format[fmt]
|
90
97
|
if format.nil?
|
91
|
-
raise "Plugin format #{
|
98
|
+
raise "Plugin format #{fmt} does not exist or is disabled!"
|
92
99
|
end
|
93
|
-
@plugins[type][name] = format.new data
|
100
|
+
@plugins[type][name] = format.new data.delete('$'),
|
94
101
|
:script_dir => @script_dir,
|
95
102
|
:data_dir => @data_dir,
|
96
103
|
:plugin_dir => @plugin_dir
|
104
|
+
@etc[type][name] = data
|
97
105
|
end
|
98
106
|
end
|
107
|
+
#Returns a hash of any extra keys in the plugin metadata file of
|
108
|
+
#the plugin with type 'type' and name 'name'. The plugin needs
|
109
|
+
#to be loaded for this to work.
|
110
|
+
def etc name, type
|
111
|
+
@etc[type][name]
|
112
|
+
end
|
99
113
|
#Unloads a plugin of name 'name' and type 'type'.
|
100
114
|
def unload name, type
|
115
|
+
@etc[type].delete name
|
101
116
|
@plugins[type].delete name
|
102
117
|
end
|
103
118
|
#Loads all plugins of type 'type'.
|
@@ -108,6 +123,7 @@ module Plugin
|
|
108
123
|
end
|
109
124
|
#Unloads all plugins of type 'type'.
|
110
125
|
def unload_type type
|
126
|
+
@etc.delete type
|
111
127
|
@plugins.delete type
|
112
128
|
end
|
113
129
|
#Loads all plugins.
|
@@ -118,6 +134,7 @@ module Plugin
|
|
118
134
|
end
|
119
135
|
#Unloads all plugins.
|
120
136
|
def unload_all
|
137
|
+
@etc.clear
|
121
138
|
@plugins.clear
|
122
139
|
end
|
123
140
|
#Excecutes the plugin of type 'type' called 'name', passing in any other arguments.
|
@@ -125,6 +142,17 @@ module Plugin
|
|
125
142
|
def run name, type, *args
|
126
143
|
@plugins[type][name].call *args
|
127
144
|
end
|
145
|
+
#Returns a list of the loaded types
|
146
|
+
def types
|
147
|
+
@plugins.keys
|
148
|
+
end
|
149
|
+
#Returns a list of the plugins of type 'type'
|
150
|
+
def plugins type
|
151
|
+
unless @plugins.has_key? type
|
152
|
+
return []
|
153
|
+
end
|
154
|
+
@plugins[type].keys
|
155
|
+
end
|
128
156
|
end
|
129
157
|
|
130
158
|
end
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: plugin-loader
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- David Green
|
@@ -9,39 +15,51 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date: 2010-09-
|
18
|
+
date: 2010-09-29 00:00:00 +01:00
|
13
19
|
default_executable:
|
14
20
|
dependencies:
|
15
21
|
- !ruby/object:Gem::Dependency
|
16
22
|
name: json
|
17
|
-
|
18
|
-
|
19
|
-
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
20
26
|
requirements:
|
21
27
|
- - ">="
|
22
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
23
32
|
version: "0"
|
24
|
-
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
25
35
|
- !ruby/object:Gem::Dependency
|
26
36
|
name: dbus
|
27
|
-
|
28
|
-
|
29
|
-
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
30
40
|
requirements:
|
31
41
|
- - ">="
|
32
42
|
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
33
46
|
version: "0"
|
34
|
-
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
35
49
|
- !ruby/object:Gem::Dependency
|
36
50
|
name: escape
|
37
|
-
|
38
|
-
|
39
|
-
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
40
54
|
requirements:
|
41
55
|
- - ">="
|
42
56
|
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
43
60
|
version: "0"
|
44
|
-
|
61
|
+
type: :runtime
|
62
|
+
version_requirements: *id003
|
45
63
|
description: |-
|
46
64
|
plugin-loader can be used to extend a program written in ruby by using plugins.
|
47
65
|
Plugins can be written in ruby or can be external resources.
|
@@ -70,21 +88,29 @@ rdoc_options: []
|
|
70
88
|
require_paths:
|
71
89
|
- lib
|
72
90
|
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
73
92
|
requirements:
|
74
93
|
- - ">="
|
75
94
|
- !ruby/object:Gem::Version
|
95
|
+
hash: 53
|
96
|
+
segments:
|
97
|
+
- 1
|
98
|
+
- 8
|
99
|
+
- 1
|
76
100
|
version: 1.8.1
|
77
|
-
version:
|
78
101
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
79
103
|
requirements:
|
80
104
|
- - ">="
|
81
105
|
- !ruby/object:Gem::Version
|
106
|
+
hash: 3
|
107
|
+
segments:
|
108
|
+
- 0
|
82
109
|
version: "0"
|
83
|
-
version:
|
84
110
|
requirements: []
|
85
111
|
|
86
112
|
rubyforge_project:
|
87
|
-
rubygems_version: 1.3.
|
113
|
+
rubygems_version: 1.3.7
|
88
114
|
signing_key:
|
89
115
|
specification_version: 3
|
90
116
|
summary: Library for creating extensible ruby applications
|