lib/console.rb in simple_console-0.0.2 vs lib/console.rb in simple_console-0.0.3
- old
+ new
@@ -13,36 +13,27 @@
module ClassMethods
def define_cmd(name, desc, &block)
commands[name.to_s] = {desc: desc, block: block}
-
- define_method(command_method_name(name), &block)
end
def run_cmd(instance, cmd_name, args = [])
cmd_name = cmd_name.to_s
+ command = commands[cmd_name]
- if commands[cmd_name]
- instance.send(command_method_name(cmd_name), *args)
+ if command
+ instance.instance_exec(*args, &command[:block])
else
puts "Invalid command '#{cmd_name}'"
end
rescue => e
puts e.message
end
def commands
@commands ||= {}
end
-
-
- private
-
- def command_method_name(cmd_name)
- "_cmd_#{cmd_name}"
- end
-
end
def start(prompt = '> ', options = {})
puts (options[:hello] || options['hello'] || "use 'help' command show all commands")