Permalink
Browse files

Refactor to use modules

  • Loading branch information...
1 parent e27e6ae commit 17893bf134d7f3cae8db3a030bf530d35cb71dc4 @mtrudel committed Jan 4, 2011
Showing with 88 additions and 72 deletions.
  1. +77 −72 lib/base.rb
  2. +11 −0 lib/menu.rb
View
@@ -1,86 +1,91 @@
require 'readline'
require 'active_support'
+require 'menu'
-class MenuTree
- attr_accessor :directory
- attr_accessor :parent
- attr_accessor :prompt
-
- def initialize(directory, parent = "")
- @directory = directory
- @parent = parent
- if @parent.is_a? self.class
- @prompt = @parent.prompt + "/" + File.basename(@directory)
- else
- @prompt = @parent
- end
- end
-
- def run_command(cmds)
- Readline.completion_append_character = " "
- Readline.completion_proc = Proc.new do |str|
- commands(str)
+module MenuTree
+ class Base
+ attr_accessor :directory
+ attr_accessor :parent
+ attr_accessor :prompt
+ attr_accessor :instance
+
+ def initialize(directory, parent = "")
+ @directory = directory
+ @parent = parent
+ if @parent.is_a? self.class
+ @prompt = @parent.prompt + "/" + File.basename(@directory)
+ else
+ @prompt = @parent
+ end
+ file = File.join(@directory, "menu.rb")
+ @instance = MenuTree::Menu.new(self)
+ @instance.instance_eval(File.read(file), file) if File.exists? file
end
- one_shot = true unless cmds.empty?
- to_run = cmds.shift
- while true do
- if (to_run.nil?)
- cmds = Readline.readline("#{@prompt}> ", true).split
- to_run = cmds.shift
+ def repl(cmds)
+ Readline.completion_append_character = " "
+ Readline.completion_proc = Proc.new do |str|
+ commands(str)
end
-
- # If it's a builtin, run it
- if (to_run.nil?)
- next
- elsif (to_run == "help" or to_run == "?")
- display_help
- elsif (to_run == "quit")
- exit
- elsif (to_run == "..")
- return
- elsif File.exists?(File.join(@directory, "#{to_run}.rb"))
- # If we have a .rb file, run it with the rest of cmd as arguments
- # TODO -- use .extend?
- require File.join(@directory, "#{to_run}")
- eval(to_run.camelize).new.run(cmds)
- elsif File.directory?(File.join(@directory, to_run))
- menu = MenuTree.new(File.join(@directory, to_run), self)
- menu.run_command(cmds)
- elsif File.exists?(File.join(@directory, "default.rb"))
- # TODO let default handle it, allowing us to have proxy menus
- else
- puts "Unknown command #{to_run}"
+
+ one_shot = true unless cmds.empty?
+ to_run = cmds.shift
+ while true do
+ if (to_run.nil?)
+ cmds = Readline.readline("#{@prompt}> ", true).split
+ to_run = cmds.shift
+ end
+
+ # If it's a builtin, run it
+ if (to_run.nil?)
+ next
+ elsif (to_run == "help" or to_run == "?")
+ display_help
+ elsif (to_run == "quit")
+ exit
+ elsif (to_run == "..")
+ return
+ elsif File.directory?(File.join(@directory, to_run))
+ menu = MenuTree::Base.new(File.join(@directory, to_run), self)
+ menu.repl(cmds)
+ else
+ @instance.send to_run, cmds rescue puts "Unknown command #{to_run}"
+ end
+ to_run = nil
+ return if one_shot
end
- to_run = nil
- return if one_shot
end
- end
-
- def commands(prefix='')
- completions = []
- Dir.new(@directory).each do |command|
- if command =~ /^[^\.]/ and command =~ /^#{Regexp.escape(prefix)}/ and File.file?(File.join(@directory, command))
- completions << File.basename(command, '.rb')
- end
+
+ def commands(prefix='')
+ (instance_commands + submenus).select { |command| command =~ /^#{Regexp.escape(prefix)}/ }
end
- completions
- end
- def display_help
- puts <<EOF
-Builtin commands:
- help:\t\t\t\tDisplays this message
- quit:\t\t\t\tQuits this session
- ..: \t\t\t\tGoes up a level in the menu (or quits if at the root)
-
-#{@parent} commands:
-EOF
+ def instance_commands
+ @instance.methods - MenuTree::Menu.instance_methods
+ end
+
+ def submenus
+ Dir.new(@directory).select { |entry| entry =~ /^[^\.]/ and File.directory?(entry) }
+ end
+
+ def display_help
+ # TODO
+ puts <<EOF
+ Builtin commands:
+ help:\t\t\t\tDisplays this message
+ quit:\t\t\t\tQuits this session
+ ..: \t\t\t\tGoes up a level in the menu (or quits if at the root)
- # TODO display some help for each submenu
- commands.each do |cmd|
- require File.join(@directory, "#{cmd}")
- puts " #{cmd}:\t\t\t\t#{eval(cmd.camelize).new.description}"
+ #{@prompt} commands:
+EOF
+ instance_commands.each do |cmd|
+ puts "#{cmd}"
+ end
+
+ puts "Submenus:"
+ submenus.each do |submenu|
+ puts "#{submenu}"
+ end
end
end
end
View
@@ -0,0 +1,11 @@
+module MenuTree
+ class Menu
+ def initialize(parent_tree)
+ @parent_tree = parent_tree
+ end
+
+ def menutree(&block)
+ self.instance_exec(@parent_tree, &block) if block_given?
+ end
+ end
+end

0 comments on commit 17893bf

Please sign in to comment.