Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/tools/capistrano/lib/capistrano/extensions.rb

Revision 6438, 1.7 kB (checked in by minam, 2 years ago)

unit tests for extensions mechanism, and a bit more robustness

Line 
1 module Capistrano
2   class ExtensionProxy #:nodoc:
3     def initialize(config, mod)
4       @config = config
5       extend(mod)
6     end
7
8     def method_missing(sym, *args, &block)
9       @config.send(sym, *args, &block)
10     end
11   end
12
13   # Holds the set of registered plugins, keyed by name (where the name is a
14   # symbol).
15   EXTENSIONS = {}
16
17   # Register the given module as a plugin with the given name. It will henceforth
18   # be available via a proxy object on Configuration instances, accessible by
19   # a method with the given name.
20   def self.plugin(name, mod)
21     name = name.to_sym
22     return false if EXTENSIONS.has_key?(name)
23
24     methods = Capistrano::Configuration.public_instance_methods +
25       Capistrano::Configuration.protected_instance_methods +
26       Capistrano::Configuration.private_instance_methods
27
28     if methods.include?(name.to_s)
29       raise Capistrano::Error, "registering a plugin named `#{name}' would shadow a method on Capistrano::Configuration with the same name"
30     end
31
32     Capistrano::Configuration.class_eval <<-STR, __FILE__, __LINE__+1
33       def #{name}
34         @__#{name}_proxy ||= Capistrano::ExtensionProxy.new(self, Capistrano::EXTENSIONS[#{name.inspect}])
35       end
36     STR
37
38     EXTENSIONS[name] = mod
39     return true
40   end
41
42   # Unregister the plugin with the given name.
43   def self.remove_plugin(name)
44     name = name.to_sym
45     if EXTENSIONS.delete(name)
46       Capistrano::Configuration.send(:remove_method, name)
47       return true
48     end
49
50     return false
51   end
52
53   def self.configuration(*args) #:nodoc:
54     warn "[DEPRECATION] Capistrano.configuration is deprecated. Use Capistrano::Configuration.instance instead"
55     Capistrano::Configuration.instance(*args)
56   end
57 end
Note: See TracBrowser for help on using the browser.