gem_plugin 0.1 → 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. data/Rakefile +15 -9
  2. data/bin/gpgen +60 -0
  3. data/doc/rdoc/classes/GemPlugin.html +3 -2
  4. data/doc/rdoc/classes/GemPlugin.src/M000001.html +5 -5
  5. data/doc/rdoc/classes/GemPlugin/Base.html +1 -1
  6. data/doc/rdoc/classes/GemPlugin/Base.src/M000002.html +6 -6
  7. data/doc/rdoc/classes/GemPlugin/Base.src/M000003.html +4 -4
  8. data/doc/rdoc/classes/GemPlugin/Base.src/M000004.html +4 -4
  9. data/doc/rdoc/classes/GemPlugin/Manager.html +107 -8
  10. data/doc/rdoc/classes/GemPlugin/Manager.src/M000005.html +9 -5
  11. data/doc/rdoc/classes/GemPlugin/Manager.src/M000006.html +30 -29
  12. data/doc/rdoc/classes/GemPlugin/Manager.src/M000007.html +5 -5
  13. data/doc/rdoc/classes/GemPlugin/Manager.src/M000008.html +15 -15
  14. data/doc/rdoc/classes/GemPlugin/Manager.src/M000009.html +5 -5
  15. data/doc/rdoc/classes/GemPlugin/Manager.src/M000010.html +28 -0
  16. data/doc/rdoc/classes/GemPlugin/Manager.src/M000011.html +28 -0
  17. data/doc/rdoc/classes/GemPlugin/PluginNotLoaded.html +111 -0
  18. data/doc/rdoc/created.rid +1 -1
  19. data/doc/rdoc/files/README.html +3 -6
  20. data/doc/rdoc/files/lib/gem_plugin_rb.html +1 -1
  21. data/doc/rdoc/fr_class_index.html +1 -0
  22. data/doc/rdoc/fr_method_index.html +3 -1
  23. data/lib/gem_plugin.rb +89 -13
  24. data/resources/COPYING +1 -0
  25. data/resources/LICENSE +1 -0
  26. data/resources/README +5 -0
  27. data/resources/Rakefile +37 -0
  28. data/resources/lib/project/init.rb +6 -0
  29. data/resources/resources/defaults.yaml +2 -0
  30. data/resources/tools/rakehelp.rb +105 -0
  31. data/test/test_plugins.rb +3 -4
  32. data/tools/rakehelp.rb +5 -11
  33. metadata +36 -11
@@ -10,20 +10,20 @@
10
10
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
11
11
  </head>
12
12
  <body class="standalone-code">
13
- <pre> <span class="ruby-comment cmt"># File lib/gem_plugin.rb, line 133</span>
14
- 133: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">create</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">options</span> = {})
15
- 134: <span class="ruby-identifier">last_slash</span> = <span class="ruby-identifier">name</span>.<span class="ruby-identifier">rindex</span>(<span class="ruby-value str">&quot;/&quot;</span>)
16
- 135: <span class="ruby-identifier">category</span> = <span class="ruby-identifier">name</span>[<span class="ruby-value">0</span> <span class="ruby-operator">...</span> <span class="ruby-identifier">last_slash</span>]
17
- 136: <span class="ruby-identifier">plugin</span> = <span class="ruby-identifier">name</span>[<span class="ruby-identifier">last_slash</span> <span class="ruby-operator">..</span> <span class="ruby-value">-1</span>]
18
- 137:
19
- 138: <span class="ruby-identifier">map</span> = <span class="ruby-ivar">@plugins</span>[<span class="ruby-identifier">category</span>]
20
- 139: <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">not</span> <span class="ruby-identifier">map</span>
21
- 140: <span class="ruby-identifier">raise</span> <span class="ruby-node">&quot;Plugin category #{category} does not exist&quot;</span>
22
- 141: <span class="ruby-keyword kw">elsif</span> <span class="ruby-keyword kw">not</span> <span class="ruby-identifier">map</span>.<span class="ruby-identifier">has_key?</span> <span class="ruby-identifier">plugin</span>
23
- 142: <span class="ruby-identifier">raise</span> <span class="ruby-node">&quot;Plugin #{plugin} does not exist in category #{category}&quot;</span>
24
- 143: <span class="ruby-keyword kw">else</span>
25
- 144: <span class="ruby-identifier">map</span>[<span class="ruby-identifier">plugin</span>].<span class="ruby-identifier">new</span>(<span class="ruby-identifier">options</span>)
26
- 145: <span class="ruby-keyword kw">end</span>
27
- 146: <span class="ruby-keyword kw">end</span></pre>
13
+ <pre> <span class="ruby-comment cmt"># File lib/gem_plugin.rb, line 145</span>
14
+ 145: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">create</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">options</span> = {})
15
+ 146: <span class="ruby-identifier">last_slash</span> = <span class="ruby-identifier">name</span>.<span class="ruby-identifier">rindex</span>(<span class="ruby-value str">&quot;/&quot;</span>)
16
+ 147: <span class="ruby-identifier">category</span> = <span class="ruby-identifier">name</span>[<span class="ruby-value">0</span> <span class="ruby-operator">...</span> <span class="ruby-identifier">last_slash</span>]
17
+ 148: <span class="ruby-identifier">plugin</span> = <span class="ruby-identifier">name</span>[<span class="ruby-identifier">last_slash</span> <span class="ruby-operator">..</span> <span class="ruby-value">-1</span>]
18
+ 149:
19
+ 150: <span class="ruby-identifier">map</span> = <span class="ruby-ivar">@plugins</span>[<span class="ruby-identifier">category</span>]
20
+ 151: <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">not</span> <span class="ruby-identifier">map</span>
21
+ 152: <span class="ruby-identifier">raise</span> <span class="ruby-node">&quot;Plugin category #{category} does not exist&quot;</span>
22
+ 153: <span class="ruby-keyword kw">elsif</span> <span class="ruby-keyword kw">not</span> <span class="ruby-identifier">map</span>.<span class="ruby-identifier">has_key?</span> <span class="ruby-identifier">plugin</span>
23
+ 154: <span class="ruby-identifier">raise</span> <span class="ruby-node">&quot;Plugin #{plugin} does not exist in category #{category}&quot;</span>
24
+ 155: <span class="ruby-keyword kw">else</span>
25
+ 156: <span class="ruby-identifier">map</span>[<span class="ruby-identifier">plugin</span>].<span class="ruby-identifier">new</span>(<span class="ruby-identifier">options</span>)
26
+ 157: <span class="ruby-keyword kw">end</span>
27
+ 158: <span class="ruby-keyword kw">end</span></pre>
28
28
  </body>
29
29
  </html>
@@ -5,14 +5,14 @@
5
5
 
6
6
  <html>
7
7
  <head>
8
- <title>available (GemPlugin::Manager)</title>
8
+ <title>loaded? (GemPlugin::Manager)</title>
9
9
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
10
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
11
11
  </head>
12
12
  <body class="standalone-code">
13
- <pre> <span class="ruby-comment cmt"># File lib/gem_plugin.rb, line 151</span>
14
- 151: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">available</span>
15
- 152: <span class="ruby-keyword kw">return</span> <span class="ruby-ivar">@plugins</span>
16
- 153: <span class="ruby-keyword kw">end</span></pre>
13
+ <pre> <span class="ruby-comment cmt"># File lib/gem_plugin.rb, line 162</span>
14
+ 162: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">loaded?</span>(<span class="ruby-identifier">gem_name</span>)
15
+ 163: <span class="ruby-ivar">@gems</span>.<span class="ruby-identifier">has_key?</span> <span class="ruby-identifier">gem_name</span>
16
+ 164: <span class="ruby-keyword kw">end</span></pre>
17
17
  </body>
18
18
  </html>
@@ -0,0 +1,28 @@
1
+ <?xml version="1.0" encoding="iso-8859-1"?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
+
6
+ <html>
7
+ <head>
8
+ <title>resource (GemPlugin::Manager)</title>
9
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
+ <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
11
+ </head>
12
+ <body class="standalone-code">
13
+ <pre> <span class="ruby-comment cmt"># File lib/gem_plugin.rb, line 185</span>
14
+ 185: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">resource</span>(<span class="ruby-identifier">gem_name</span>, <span class="ruby-identifier">path</span>)
15
+ 186: <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">not</span> <span class="ruby-identifier">loaded?</span> <span class="ruby-identifier">gem_name</span>
16
+ 187: <span class="ruby-identifier">raise</span> <span class="ruby-constant">PluginNotLoaded</span>.<span class="ruby-identifier">new</span>(<span class="ruby-node">&quot;Plugin #{gem_name} not loaded when getting resource #{path}&quot;</span>)
17
+ 188: <span class="ruby-keyword kw">end</span>
18
+ 189:
19
+ 190: <span class="ruby-identifier">file</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">join</span>(<span class="ruby-ivar">@gems</span>[<span class="ruby-identifier">gem_name</span>], <span class="ruby-value str">&quot;resources&quot;</span>, <span class="ruby-identifier">path</span>)
20
+ 191:
21
+ 192: <span class="ruby-keyword kw">if</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">exist?</span> <span class="ruby-identifier">file</span>
22
+ 193: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">file</span>
23
+ 194: <span class="ruby-keyword kw">else</span>
24
+ 195: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span>
25
+ 196: <span class="ruby-keyword kw">end</span>
26
+ 197: <span class="ruby-keyword kw">end</span></pre>
27
+ </body>
28
+ </html>
@@ -0,0 +1,28 @@
1
+ <?xml version="1.0" encoding="iso-8859-1"?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
+
6
+ <html>
7
+ <head>
8
+ <title>config (GemPlugin::Manager)</title>
9
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
+ <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
11
+ </head>
12
+ <body class="standalone-code">
13
+ <pre> <span class="ruby-comment cmt"># File lib/gem_plugin.rb, line 218</span>
14
+ 218: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">config</span>(<span class="ruby-identifier">gem_name</span>, <span class="ruby-identifier">options</span>={})
15
+ 219: <span class="ruby-identifier">config_file</span> = <span class="ruby-constant">Manager</span>.<span class="ruby-identifier">instance</span>.<span class="ruby-identifier">resource</span>(<span class="ruby-identifier">gem_name</span>, <span class="ruby-value str">&quot;/defaults.yaml&quot;</span>)
16
+ 220: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">config_file</span>
17
+ 221: <span class="ruby-keyword kw">begin</span>
18
+ 222: <span class="ruby-identifier">defaults</span> = <span class="ruby-constant">YAML</span>.<span class="ruby-identifier">load_file</span>(<span class="ruby-identifier">config_file</span>)
19
+ 223: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">defaults</span>.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">options</span>)
20
+ 224: <span class="ruby-keyword kw">rescue</span>
21
+ 225: <span class="ruby-identifier">raise</span> <span class="ruby-node">&quot;Error loading config #{config_file} for gem #{gem_name}&quot;</span>
22
+ 226: <span class="ruby-keyword kw">end</span>
23
+ 227: <span class="ruby-keyword kw">else</span>
24
+ 228: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">options</span>
25
+ 229: <span class="ruby-keyword kw">end</span>
26
+ 230: <span class="ruby-keyword kw">end</span></pre>
27
+ </body>
28
+ </html>
@@ -0,0 +1,111 @@
1
+ <?xml version="1.0" encoding="iso-8859-1"?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
+
6
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7
+ <head>
8
+ <title>Class: GemPlugin::PluginNotLoaded</title>
9
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
+ <meta http-equiv="Content-Script-Type" content="text/javascript" />
11
+ <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
12
+ <script type="text/javascript">
13
+ // <![CDATA[
14
+
15
+ function popupCode( url ) {
16
+ window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
17
+ }
18
+
19
+ function toggleCode( id ) {
20
+ if ( document.getElementById )
21
+ elem = document.getElementById( id );
22
+ else if ( document.all )
23
+ elem = eval( "document.all." + id );
24
+ else
25
+ return false;
26
+
27
+ elemStyle = elem.style;
28
+
29
+ if ( elemStyle.display != "block" ) {
30
+ elemStyle.display = "block"
31
+ } else {
32
+ elemStyle.display = "none"
33
+ }
34
+
35
+ return true;
36
+ }
37
+
38
+ // Make codeblocks hidden by default
39
+ document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
40
+
41
+ // ]]>
42
+ </script>
43
+
44
+ </head>
45
+ <body>
46
+
47
+
48
+
49
+ <div id="classHeader">
50
+ <table class="header-table">
51
+ <tr class="top-aligned-row">
52
+ <td><strong>Class</strong></td>
53
+ <td class="class-name-in-header">GemPlugin::PluginNotLoaded</td>
54
+ </tr>
55
+ <tr class="top-aligned-row">
56
+ <td><strong>In:</strong></td>
57
+ <td>
58
+ <a href="../../files/lib/gem_plugin_rb.html">
59
+ lib/gem_plugin.rb
60
+ </a>
61
+ <br />
62
+ </td>
63
+ </tr>
64
+
65
+ <tr class="top-aligned-row">
66
+ <td><strong>Parent:</strong></td>
67
+ <td>
68
+ StandardError
69
+ </td>
70
+ </tr>
71
+ </table>
72
+ </div>
73
+ <!-- banner header -->
74
+
75
+ <div id="bodyContent">
76
+
77
+
78
+
79
+ <div id="contextContent">
80
+
81
+
82
+
83
+ </div>
84
+
85
+
86
+ </div>
87
+
88
+
89
+ <!-- if includes -->
90
+
91
+ <div id="section">
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+ <!-- if method_list -->
101
+
102
+
103
+ </div>
104
+
105
+
106
+ <div id="validator-badges">
107
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
108
+ </div>
109
+
110
+ </body>
111
+ </html>
@@ -1 +1 @@
1
- Mon Mar 06 00:45:39 EST 2006
1
+ Sat Mar 11 22:25:45 EST 2006
@@ -92,9 +92,7 @@ only have to require &#8216;gem_plugin&#8217; and then use the <a
92
92
  href="../classes/GemPlugin/Manager.html#M000008">GemPlugin::Manager.create</a>,
93
93
  <a
94
94
  href="../classes/GemPlugin/Manager.html#M000006">GemPlugin::Manager.load</a>,
95
- and <a
96
- href="../classes/GemPlugin/Manager.html#M000009">GemPlugin::Manager.available</a>
97
- methods to work with them.
95
+ and GemPlugin::Manager.available methods to work with them.
98
96
  </p>
99
97
  <ul>
100
98
  <li><a
@@ -108,9 +106,8 @@ href="../classes/GemPlugin/Manager.html#M000008">GemPlugin::Manager.create</a>
108
106
  &#8212; Takes a URI style name and some options then creates one for you.
109
107
 
110
108
  </li>
111
- <li><a
112
- href="../classes/GemPlugin/Manager.html#M000009">GemPlugin::Manager.available</a>
113
- &#8212; Lets you inspect and mess with the internal plugin registry.
109
+ <li>GemPlugin::Manager.available &#8212; Lets you inspect and mess with the
110
+ internal plugin registry.
114
111
 
115
112
  </li>
116
113
  </ul>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Sun Mar 05 22:03:34 EST 2006</td>
59
+ <td>Sat Mar 11 21:19:15 EST 2006</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -23,6 +23,7 @@
23
23
  <a href="classes/GemPlugin.html">GemPlugin</a><br />
24
24
  <a href="classes/GemPlugin/Base.html">GemPlugin::Base</a><br />
25
25
  <a href="classes/GemPlugin/Manager.html">GemPlugin::Manager</a><br />
26
+ <a href="classes/GemPlugin/PluginNotLoaded.html">GemPlugin::PluginNotLoaded</a><br />
26
27
  </div>
27
28
  </div>
28
29
  </body>
@@ -21,14 +21,16 @@
21
21
  <h1 class="section-bar">Methods</h1>
22
22
  <div id="index-entries">
23
23
  <a href="classes/GemPlugin.html#M000001">Plugin (GemPlugin)</a><br />
24
- <a href="classes/GemPlugin/Manager.html#M000009">available (GemPlugin::Manager)</a><br />
25
24
  <a href="classes/GemPlugin/Base.html#M000003">category= (GemPlugin::Base)</a><br />
25
+ <a href="classes/GemPlugin/Manager.html#M000011">config (GemPlugin::Manager)</a><br />
26
26
  <a href="classes/GemPlugin/Manager.html#M000008">create (GemPlugin::Manager)</a><br />
27
27
  <a href="classes/GemPlugin/Base.html#M000002">inherited (GemPlugin::Base)</a><br />
28
28
  <a href="classes/GemPlugin/Manager.html#M000006">load (GemPlugin::Manager)</a><br />
29
+ <a href="classes/GemPlugin/Manager.html#M000009">loaded? (GemPlugin::Manager)</a><br />
29
30
  <a href="classes/GemPlugin/Manager.html#M000005">new (GemPlugin::Manager)</a><br />
30
31
  <a href="classes/GemPlugin/Base.html#M000004">new (GemPlugin::Base)</a><br />
31
32
  <a href="classes/GemPlugin/Manager.html#M000007">register (GemPlugin::Manager)</a><br />
33
+ <a href="classes/GemPlugin/Manager.html#M000010">resource (GemPlugin::Manager)</a><br />
32
34
  </div>
33
35
  </div>
34
36
  </body>
@@ -20,9 +20,9 @@ require 'rubygems'
20
20
  # gems based on what they also depend on, thus letting you load these gems when appropriate.
21
21
  #
22
22
  # Since this system was written originally for the Mongrel project that'll be the
23
- # best examle of using it.
23
+ # best example of using it.
24
24
  #
25
- # Imagine you have a neat plugin for Mongrel called snazzy_command that give the
25
+ # Imagine you have a neat plugin for Mongrel called snazzy_command that gives the
26
26
  # mongrel_rails a new command snazzy (like: mongrel_rails snazzy). You'd like
27
27
  # people to be able to grab this plugin if they want and use it, because it's snazzy.
28
28
  #
@@ -51,6 +51,9 @@ module GemPlugin
51
51
 
52
52
  EXCLUDE = true
53
53
  INCLUDE = false
54
+
55
+ class PluginNotLoaded < StandardError; end
56
+
54
57
 
55
58
  # This class is used by people who use gem plugins (but don't necessarily make them)
56
59
  # to add plugins to their own systems. It provides a way to load plugins, list them,
@@ -59,10 +62,17 @@ module GemPlugin
59
62
  # It is a singleton so you use like this: GemPlugins::Manager.instance.load
60
63
  class Manager
61
64
  include Singleton
65
+ attr_reader :plugins
66
+ attr_reader :gems
67
+
62
68
 
63
69
  def initialize
70
+ # plugins that have been loaded
64
71
  @plugins = {}
65
- @loaded_gems = []
72
+
73
+ # keeps track of gems which have been loaded already by the manager *and*
74
+ # where they came from so that they can be referenced later
75
+ @gems = {}
66
76
  end
67
77
 
68
78
 
@@ -95,7 +105,7 @@ module GemPlugin
95
105
 
96
106
  gems.each do |path, gem|
97
107
  # don't load gems more than once
98
- next if @loaded_gems.include? gem.name
108
+ next if @gems.has_key? gem.name
99
109
  check = needs.dup
100
110
 
101
111
  # rolls through the depends and inverts anything it finds
@@ -110,11 +120,12 @@ module GemPlugin
110
120
  # makes them false so we'll skip this gem if any excludes are found
111
121
  if (check.select {|name,test| !test}).length == 0
112
122
  # looks like no needs were set to false, so it's good
113
- require_gem gem.name
114
- @loaded_gems << gem.name
123
+ require "#{gem.name}/init"
124
+ @gems[gem.name] = File.join(Gem.dir, "gems", "#{gem.name}-#{gem.version}")
115
125
  end
116
-
117
126
  end
127
+
128
+ return nil
118
129
  end
119
130
 
120
131
 
@@ -125,7 +136,8 @@ module GemPlugin
125
136
  @plugins[category] ||= {}
126
137
  @plugins[category][name.downcase] = klass
127
138
  end
128
-
139
+
140
+
129
141
  # Resolves the given name (should include /category/name) to
130
142
  # find the plugin class and create an instance. You can
131
143
  # pass a second hash option that is then given to the Plugin
@@ -146,15 +158,79 @@ module GemPlugin
146
158
  end
147
159
 
148
160
 
149
- # Returns a map of URIs->{"name" => Plugin} that you can
150
- # use to investigate available handlers.
151
- def available
152
- return @plugins
161
+ # Simply says whether the given gem has been loaded yet or not.
162
+ def loaded?(gem_name)
163
+ @gems.has_key? gem_name
153
164
  end
165
+
166
+
167
+ # GemPlugins can have a 'resources' directory which is packaged with them
168
+ # and can hold any data resources the plugin may need. The main problem
169
+ # is that it's difficult to figure out where these resources are
170
+ # actually located on the file system. The resource method tries to
171
+ # locate the real path for a given resource path.
172
+ #
173
+ # Let's say you have a 'resources/stylesheets/default.css' file in your
174
+ # gem distribution, then finding where this file really is involves:
175
+ #
176
+ # Manager.instance.resource("mygem", "/stylesheets/default.css")
177
+ #
178
+ # You either get back the full path to the resource or you get a nil
179
+ # if it doesn't exist.
180
+ #
181
+ # If you request a path for a GemPlugin that hasn't been loaded yet
182
+ # then it will throw an PluginNotLoaded exception. The gem may be
183
+ # present on your system in this case, but you just haven't loaded
184
+ # it with Manager.instance.load properly.
185
+ def resource(gem_name, path)
186
+ if not loaded? gem_name
187
+ raise PluginNotLoaded.new("Plugin #{gem_name} not loaded when getting resource #{path}")
188
+ end
189
+
190
+ file = File.join(@gems[gem_name], "resources", path)
191
+
192
+ if File.exist? file
193
+ return file
194
+ else
195
+ return nil
196
+ end
197
+ end
198
+
154
199
 
200
+ # While Manager.resource will find arbitrary resources, a special
201
+ # case is when you need to load a set of configuration defaults.
202
+ # GemPlugin normalizes this to be if you have a file "resources/defaults.yaml"
203
+ # then you'll be able to load them via Manager.config.
204
+ #
205
+ # How you use the method is you get the options the user wants set, pass
206
+ # them to Manager.instance.config, and what you get back is a new Hash
207
+ # with the user's settings overriding the defaults.
208
+ #
209
+ # opts = Manager.instance.config "mygem", :age => 12, :max_load => .9
210
+ #
211
+ # In the above case, if defaults had {:age => 14} then it would be
212
+ # changed to 12.
213
+ #
214
+ # This loads the .yaml file on the fly every time, so doing it a
215
+ # whole lot is very stupid. If you need to make frequent calls to
216
+ # this, call it once with no options (Manager.instance.config) then
217
+ # use the returned defaults directly from then on.
218
+ def config(gem_name, options={})
219
+ config_file = Manager.instance.resource(gem_name, "/defaults.yaml")
220
+ if config_file
221
+ begin
222
+ defaults = YAML.load_file(config_file)
223
+ return defaults.merge(options)
224
+ rescue
225
+ raise "Error loading config #{config_file} for gem #{gem_name}"
226
+ end
227
+ else
228
+ return options
229
+ end
230
+ end
155
231
  end
156
232
 
157
- # This base class for plugins reallys does nothing
233
+ # This base class for plugins really does nothing
158
234
  # more than wire up the new class into the right category.
159
235
  # It is not thread-safe yet but will be soon.
160
236
  class Base