Skip to content

Instantly share code, notes, and snippets.

@trinary
Created September 30, 2011 17:30
Show Gist options
  • Save trinary/1254432 to your computer and use it in GitHub Desktop.
Save trinary/1254432 to your computer and use it in GitHub Desktop.
#!/bin/env ruby
class SmithCommands
attr_accessor :plugins
def initialize
@plugins = []
end
def add(plugin)
@plugins << plugin
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
def usage
puts "USAGE: \n"
puts plugins.map(&:usage).join "\n"
puts "\n\n"
end
end
class Buttle
def matches?(line)
line.include? "Smith"
end
def run(line)
puts "You rang?"
end
def usage
"Does a thing when it sees Smith. Trigger: 'Smith'"
end
end
class Jerk
def matches?(line)
line.include? "HEY"
end
def run(line)
puts "WHAT THE HELL DO YOU WANT?"
end
def usage
"Yells at you when you are rude. Trigger: 'HEY'"
end
end
class Adder
attr_accessor :num
def initialize
@num = 0
end
def matches?(line)
line.include? "add" or line.include? "reset"
end
def run(line)
add if line.include? "add"
reset if line.include? "reset"
end
def add
@num = @num + 1
puts "Ok. Current value is #{num}"
end
def reset
@num = 0
puts "Ok. Current value reset to #{num}"
end
def usage
"Keeps a counter"
end
end
commands = SmithCommands.new
#put this in a require loop
commands.add Buttle.new
commands.add Jerk.new
commands.add Adder.new
commands.usage
["Hey Smith", "HEY ASSHOLE!", "add","add","reset","bla bla bla","add"].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