Created
September 30, 2011 17:21
-
-
Save trinary/1254405 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/bin/env ruby | |
class SmithCommands | |
attr_accessor :plugins | |
def initialize | |
@plugins = [] | |
end | |
def check_and_run(msg) | |
@plugins.each do |p| | |
if p.matches? msg | |
p.run(msg) | |
return | |
end | |
end | |
not_found | |
end | |
def not_found | |
puts "I didn't find a matching plugin." | |
end | |
end | |
module CommandMixin | |
def add_to_commands(commands) | |
puts "adding #{self.class} to commands" | |
commands.plugins << self | |
end | |
end | |
class Buttle | |
include CommandMixin | |
def initialize(commands) | |
add_to_commands(commands) | |
end | |
def matches?(line) | |
true if line.include? "Smith" | |
end | |
def run(line) | |
puts "You rang?" | |
end | |
end | |
class Jerk | |
include CommandMixin | |
def initialize(commands) | |
add_to_commands(commands) | |
end | |
def matches?(line) | |
line.include? "HEY" | |
end | |
def run(line) | |
puts "WHAT THE HELL DO YOU WANT?" | |
end | |
end | |
commands = SmithCommands.new | |
Buttle.new(commands) | |
Jerk.new(commands) | |
["Hey Smith", "HEY ASSHOLE!", "bla bla bla"].each do |m| | |
puts "Checking and running for #{m}" | |
commands.check_and_run(m) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment