bin/rurema in myrurema-0.0.1 vs bin/rurema in myrurema-0.0.2

- old
+ new

@@ -41,38 +41,49 @@ } o.on("--rubyver=STR", "specify Ruby version (default: #{@rubyver})"){|str| @rubyver = str } + o.on("--version", + "show version of myrurema"){ + puts MyRurema::VERSION + exit + } o.on("--help", "show this message"){ puts o exit } } - @query = @optionparser.parse(argv).join(" ") + @query, @num = @optionparser.parse(argv) + @query = "" if @query.nil? + @num = @num.to_i if @num end attr_reader :dry_run, :ruremadir, :rubyver, :open_browser - attr_reader :command, :query + attr_reader :command, :query, :num def usage puts @optionparser end end -class Rurema +class MyRurema + VERSION = File.read((Pathname(__FILE__).dirname/"../VERSION").expand_path) SVN_URL = "http://jp.rubyist.net/svn/rurema" def initialize(opt=Options.new(ARGV)) @opt = opt end def run - if @opt.command + case + when @opt.command send(@opt.command) - elsif @opt.query.empty? + when @opt.query.empty? @opt.usage + when @opt.num + search_num(@opt.query, @opt.num, @opt.rubyver) else search(@opt.query, @opt.rubyver) end end @@ -86,24 +97,30 @@ sh "svn up #{doctree_path}" refresh_db(@opt.rubyver) end def search(query, ver) - unless has_db?(ver) - puts "You don't have a database for ruby #{ver}." - puts "Make it now? [y/n]" - if $stdin.gets.chomp.downcase == "y" - init_db(ver) - else - exit - end - end + should_have_db(ver) sh "#{bitclust_path/'bin/refe.rb'}" + " #{query} -d #{db_path(ver)}", :silent => true end + def search_num(query, num, ver) + should_have_db(ver) + + result = `#{bitclust_path/'bin/refe.rb'} #{query} -d #{db_path(ver)}` + word = result.split[num-1] + if word + word.gsub!(/\.#/, ".") # avoid multi-hit for a module function + puts "searching #{word}" + search(word, ver) + else + error "less than #{num} entries found" + end + end + def server th = Thread.new{ sh "#{bitclust_path/'standalone.rb'}" + " --baseurl=http://localhost:9292" + " --port=9292" + @@ -140,10 +157,22 @@ def doctree_path @opt.ruremadir / "doctree" end + def should_have_db(ver) + unless has_db?(ver) + puts "You don't have a database for ruby #{ver}." + puts "Make it now? [y/n]" + if $stdin.gets.chomp.downcase == "y" + init_db(ver) + else + exit + end + end + end + def has_db?(ver) db_path(ver).directory? end def db_path(ver) @@ -152,8 +181,12 @@ def sh(cmd, opt={}) puts cmd unless opt[:silent] system cmd unless @opt.dry_run end + + def error(msg) + $stderr.puts msg + end end -Rurema.new.run +MyRurema.new.run