Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created February 4, 2010 10:59
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 JoshCheek/294520 to your computer and use it in GitHub Desktop.
Save JoshCheek/294520 to your computer and use it in GitHub Desktop.
# possible solution for ruby-forum.com/topic/203515
$stdin = DATA # uses text after __END__ as input
require 'rubygems' # fixes load path on some (read: most) systems. read ruby-forum.com/topic/203245 to see why it is evil
require 'active_support/inflector' # if you have Rails, you have this library. If not: http://gemcutter.org/gems/activesupport
# the base functionality that you wish to give to the classes that implement the commands
module Command
attr_accessor :params
# in reality, initialize would probably go into your classes that include this module
# I wasn't able to override this method, not sure why (you can see I was able to override run)
def initialize( params = Array.new )
self.params = params
end
# code to do whatever you're wanting
# in this example, just shows a sort of inspection of this object, so you can see more internally what happens
def run
"Command: #{self.class}\n" \
"Params: #{params.inspect}"
end
# if you want, you can register all the commands with something like this
@registered_commands = Array.new
def self.included( base )
@registered_commands << base
end
def self.list
text_list = @registered_commands.map { |command| "* #{command}\n" }.join
"The available commands are:\n" \
"#{text_list.downcase}"
end
end
# three different command types ( what your other users will create )
class Update
include Command
end
class Edit
include Command
end
class Execute
include Command
# custom Execute code
def upcased_params
params.map { |param| param.upcase }
end
# a custom run method, overrides Command's
def run
"Command: Execute\n" \
"Modified Params: #{ upcased_params.inspect }"
end
end
puts Command.list
puts
puts 'Now Processing The Input:' , '-'*20
# pull them from the command line, turn first word into the command
# create instance of it, pass rest of the commands as arg list, call #run on it
while input = gets
next if input =~ /^\s*$/ # skip blank lines
begin
puts "Input was: #{input.inspect}" # display context
inputs = input.split # ie 'update user' becomes [ 'update' , 'user' ]
class_name = inputs.shift # class_name becomes 'update' , inputs becomes [ 'user' ]
cmd_class = class_name.classify.constantize # cmd_class becomes Update
cmd = cmd_class.new inputs # cmd becomes instance of Update
puts cmd.run # execute the run method (which prints out the state of the object)
puts
rescue NameError
puts "#{class_name} is not a valid command" # if they enter an invalid command
puts
end
end
# this is the example input that will get fed into this file ( because of line 1 )
__END__
update user
edit author 12
ecksecute ... was this the command name?
execute I guess it wasn't, lets try this one
edit my paper four me?
create a new universe
execute that thing I stored earlier
edit user1 15
update
etcetera ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment