Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@chikamichi
Created March 21, 2010 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chikamichi/339028 to your computer and use it in GitHub Desktop.
Save chikamichi/339028 to your computer and use it in GitHub Desktop.
plugins system: nested
# ./base.rb
require 'base/plugins'
module Base
class Server
include Plugins
def say(what)
p "say: #{what}"
@config = "default"
end
end
end
s = Base::Server.new
puts s.instance_eval("class << self; self; end").ancestors
puts
s.say("hello world")
puts
s.activate "something"
puts s.instance_eval("class << self; self; end").ancestors
puts
s.say("hello world")
puts
$ tree
.
|-- base
| |-- plugins
| | `-- something.rb
| `-- plugins.rb
`-- base.rb
$ ruby base.rb
Base::Server
Base::Plugins
Object
Kernel
"say: hello world"
Base::Server
Base::Plugins
Object
Kernel
"say: hello world"
# ./base/plugins.rb
module Base
module Plugins
def plugins
@plugins ||= []
end
def activate plugin
require 'base/plugins/' + plugin
plugins << plugin
plugin = Base::Plugins.const_get(plugin.capitalize)
plugin.activate if plugin.respond_to?(:activate)
end
end
end
# ./base/plugins/something.rb
module Base
module Plugins
module Something
def self.activate
Base::Server.send :extend, Redefinitions
end
module Redefinitions
def process(document)
p "Under control of Something:"
super
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment