Changeset 385 for lang/ruby/module-pluggable
- Timestamp:
- 10/06/07 08:55:05 (4 years ago)
- Location:
- lang/ruby/module-pluggable
- Files:
-
- 4 modified
-
README (modified) (1 diff)
-
Rakefile (modified) (2 diffs)
-
examples/simple.rb (modified) (1 diff)
-
lib/module/pluggable.rb (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/ruby/module-pluggable/README
r366 r385 1 README for module-pluggable 2 =========================== 1 = README for module-pluggable 3 2 4 see examples. 3 module-pluggable provides plugin system for classes. 4 5 == Installation 6 7 === Archive Installation 8 9 rake install 10 11 === Gem Installation 12 13 gem install module-pluggable 14 15 === Subversion Repository 16 17 Hosted by CodeRepos[http://coderepos.org/share/browser/lang/ruby/module-pluggable] 18 19 svn co http://svn.coderepos.org/share/lang/ruby/module-pluggable/ 20 21 == Examples 22 23 class APluggableClass 24 pluggable :plugins 25 26 def initialize 27 plugins.init(self) 28 end 29 30 def say 31 plugins.say("hello") 32 end 33 end 34 35 see examples/simple.rb. 36 37 == Copyright 38 39 Author:: cho45 <cho45@lowreal.net> 40 Copyright:: copyright (c) 2007 cho45 www.lowreal.net 41 License:: Ruby's -
lang/ruby/module-pluggable/Rakefile
r383 r385 45 45 s.has_rdoc = true 46 46 s.extra_rdoc_files = ["README", "Changelog"] 47 s.rdoc_options += RDOC_OPTS + ['--exclude', '^(ex amples|extras)/']47 s.rdoc_options += RDOC_OPTS + ['--exclude', '^(extras)/'] 48 48 s.summary = DESCRIPTION 49 49 s.description = DESCRIPTION … … 95 95 else 96 96 rdoc.rdoc_files.include('README', 'Changelog') 97 rdoc.rdoc_files.include('examples/simple.rb') 97 98 rdoc.rdoc_files.include('lib/**/*.rb') 98 99 rdoc.rdoc_files.include('ext/**/*.c') -
lang/ruby/module-pluggable/examples/simple.rb
r366 r385 3 3 require "module/pluggable" 4 4 5 class SimplePluggable 6 # pluggable [name=:plugins] [opts] 7 # pluggable make the name of instance method 8 # returning instance of Module::Pluggable::Plugin. 9 # 10 # All loaded plugins are in anonymous module, 11 # so you can't access the classes directly, 12 # and you can create some plugin-sets 13 # without confusing class variables etc. 14 pluggable 5 module Example 15 6 16 def initialize 7 class SimplePluggable 8 # pluggable [name=:plugins] [opts] 9 # pluggable make the name of instance method 10 # returning instance of Module::Pluggable::Plugin. 11 # 12 # All loaded plugins are in anonymous module, 13 # so you can't access the classes directly, 14 # and you can create some plugin-sets 15 # without confusing class variables etc. 16 pluggable 17 18 # plugins.init(self) 19 # 17 20 # `init' method is not defined on Module::Pluggable::Plugin. 18 21 # undefined methods are delegated to `call' the plugins. 19 22 # In this case, `plugins.init(self)' is same as `plugins.call(:init, self)'. 20 plugins.init(self) 23 def initialize 24 plugins.init(self) 25 end 26 27 def say 28 plugins.each do |name, instance| 29 puts instance.say 30 end 31 end 21 32 end 22 33 23 def say24 plugins.each do |name, instance|25 puts instance.say26 end27 end28 34 end 29 35 30 SimplePluggable.new.say36 Example::SimplePluggable.new.say -
lang/ruby/module-pluggable/lib/module/pluggable.rb
r368 r385 1 2 #require "module/pluggable" 1 # Author:: cho45 <cho45@lowreal.net> 2 # Copyright:: copyright (c) 2007 cho45 www.lowreal.net 3 # License:: Ruby's 3 4 4 5 require "pathname" … … 11 12 }.freeze 12 13 14 # pluggable make the name of instance method 15 # returning instance of Module::Pluggable::Plugin. 16 # 17 # All loaded plugins are in anonymous module, 18 # so you can't access the classes directly, 19 # and you can create some plugin-sets 20 # without confusing class variables etc. 21 # 22 # opts => { 23 # :search_path => name, 24 # :base_class => nil, 25 # :except => /_$/, # not yet 26 # } 13 27 def pluggable(name=:plugins, o={}) 14 28 opts = DEFAULT_OPTS.merge(o) … … 44 58 end 45 59 60 # Load +klass_name+. 61 # The plugin is loaded in anonymous module not order to 62 # destroy the environments. 63 # And remember loaded time for reloading. 64 # 65 # plugin filename must be interconversion with its class name. 66 # In this class, the conversion is do with +file2klass+/+klass2file+ methods. 46 67 def load(klass_name) 47 68 return if @plugins.include?(klass_name) … … 74 95 end 75 96 97 # Get instance of +klass_name+ plugin. 76 98 def [](klass_name) 77 99 @plugins[klass_name][:instance] if @plugins.key?(klass_name) 78 100 end 79 101 102 # Unload +klass_name 80 103 def unload(klass_name) 81 104 if @plugins.key?(klass_name) … … 85 108 end 86 109 110 # Reload +klass_name+ or 111 # load unloaded plugins or 112 # reload modified plugins. 113 # returns [loaded, unloaded] 87 114 def reload(klass_name=nil) 88 115 if klass_name … … 107 134 end 108 135 136 # Unload all plugins and reload it. 109 137 def force_reload 110 138 call(:on_unload) … … 113 141 end 114 142 143 # Iterates with plugin name and its instance. 115 144 def each(&block) 116 145 @plugins.each do |k,v| … … 119 148 end 120 149 150 # Call +name+ method of each plugins with +args+ 151 # and returns Hash of the result and its plugin name. 121 152 def call(name, *args) 122 153 ret = {} … … 127 158 end 128 159 160 # Undefined methods are delegated to each plugins. 161 # This is alias of +call+ 129 162 def method_missing(name, *args) 130 163 call(name, *args) 131 164 end 132 165 133 # foo/foo_bar => Foo::FooBar 166 private 167 # convert foo/foo_bar to Foo::FooBar 134 168 def file2klass(str) 135 169 str.split("/").map {|c| … … 138 172 end 139 173 140 # Foo::FooBar =>foo/foo_bar174 # convert Foo::FooBar to foo/foo_bar 141 175 def klass2file(str) 142 176 str.split(/::/).map {|c|
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)