Created
March 21, 2010 02:12
-
-
Save chikamichi/339028 to your computer and use it in GitHub Desktop.
plugins system: nested
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ./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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ./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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ./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