class MyRurema

Constants

SVN_URL
TMP_FILE
VERSION

Public Class Methods

new(opt=Options.new(ARGV)) click to toggle source
# File lib/myrurema.rb, line 19
def initialize(opt=Options.new(ARGV))
  @opt = opt
end

Public Instance Methods

init() click to toggle source
# File lib/myrurema.rb, line 49
def init
  sh "svn co -rHEAD #{SVN_URL}/doctree/trunk #{doctree_path}"
  sh "svn co -rHEAD #{SVN_URL}/bitclust/trunk #{bitclust_path}"
  init_db(@opt.rubyver)
end
list() click to toggle source
# File lib/myrurema.rb, line 107
def list
  should_have_db(@opt.rubyver)

  sh "#{bitclust_path/'bin/refe'}" +
       " -l -d #{db_path(@opt.rubyver)}", :silent => true
end
preview() click to toggle source
# File lib/myrurema.rb, line 139
def preview
  file, target = *@opt.rest_args

  if file
    error "file not found: #{file}" unless File.exist?(file)

    result = sh "#{bitclust_path/'tools/bc-tohtml.rb'}" +
                  " #{file}" +
                  (target ? " --target=#{target}" : "") +
                  " --ruby=#{@opt.rubyver}" +
                  " > #{TMP_FILE}"

    if result && @opt.open_browser
      cmd = (/mswin/ =~ RUBY_PLATFORM) ? "start" : "open"
      sh "#{cmd} #{TMP_FILE}"
    end
  else
    sh "cd #{doctree_path/'refm/api/src'}"
  end
end
run() click to toggle source
# File lib/myrurema.rb, line 23
def run
  if @opt.command
    send(@opt.command)
  else
    query = @opt.rest_args
    num = if !query.empty? and query.last =~ /\A\d+\z/
            query.pop.to_i
          else
            nil
          end

    case
    when query.empty?
      if num
        search(num, @opt.rubyver)
      else
        @opt.usage
      end
    when query && num
      search_num(query, num, @opt.rubyver)
    else
      search(query, @opt.rubyver)
    end
  end
end
search_num(query, num, ver) click to toggle source
# File lib/myrurema.rb, line 93
def search_num(query, num, ver)
  should_have_db(ver)

  result = %x`#{bitclust_path/'bin/refe'} #{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
server() click to toggle source
# File lib/myrurema.rb, line 114
def server
  port = @opt.port || default_port(@opt.rubyver)
  th = Thread.new{
    sh "ruby #{bitclust_path/'standalone.rb'}" +
         " --srcdir=#{bitclust_path}" +
         " --baseurl=http://localhost:#{port}" +
         " --port=#{port}" +
         " --database=#{db_path(@opt.rubyver)}" +
         " --debug" # needed to avoid the server running as daemon :-(
  }

  url = "http://localhost:#{port}/view/"
  puts "Starting BitClust server .."
  puts "Open #{url} in your browser."
  puts

  if @opt.open_browser
    sleep 1  # wait for the server to start
    Launchy.open(url)
  end

  th.join
end
update() click to toggle source
# File lib/myrurema.rb, line 55
def update
  sh "svn up #{doctree_path}"
  refresh_db(@opt.rubyver)
end

Private Instance Methods

bitclust_path() click to toggle source
# File lib/myrurema.rb, line 194
def bitclust_path
  @opt.ruremadir / "bitclust"
end
candidate_list_responded?(lines, query) click to toggle source
# File lib/myrurema.rb, line 162
def candidate_list_responded?(lines, query)
  return false if @opt.no_ask

  # if first several words are results of AND search
  lines.first(2).join.split(/\s+/).all? do |word|
    Array(query).all? do |q|
      word =~ Regexp.new(q, 'i')
    end
  end
end
db_path(ver) click to toggle source
# File lib/myrurema.rb, line 218
def db_path(ver)
  @opt.ruremadir / "db" / ver
end
default_port(ver) click to toggle source
# File lib/myrurema.rb, line 173
def default_port(ver)
  "10" + ver.scan(/\d/).join
end
doctree_path() click to toggle source
# File lib/myrurema.rb, line 198
def doctree_path
  @opt.ruremadir / "doctree"
end
error(msg) click to toggle source
# File lib/myrurema.rb, line 233
def error(msg)
  $stderr.puts msg unless $MYRUREMA_TEST
  exit
end
has_db?(ver) click to toggle source
# File lib/myrurema.rb, line 214
def has_db?(ver)
  db_path(ver).directory?
end
init_db(ver) click to toggle source
# File lib/myrurema.rb, line 177
def init_db(ver)
  sh "ruby -I #{bitclust_path/'lib'}" +
        " #{bitclust_path/'bin/bitclust'}" +
        " -d #{db_path(ver)} init version=#{ver} encoding=utf-8"

  refresh_db(ver)
end
puts(str="") click to toggle source
# File lib/myrurema.rb, line 238
def puts(str="")
  Kernel.puts(str) unless $MYRUREMA_TEST
end
refresh_db(ver) click to toggle source
# File lib/myrurema.rb, line 185
def refresh_db(ver)
  puts "Updating Rurema database:"
  puts "This will take a few minutes. Please be patient."
  sh "ruby -I #{bitclust_path/'lib'}" +
        " #{bitclust_path/'bin/bitclust'}" +
        " -d #{db_path(ver)}" +
        " update --stdlibtree=#{doctree_path/'refm/api/src'}"
end
sh(cmd, opt={}, &block) click to toggle source
# File lib/myrurema.rb, line 222
def sh(cmd, opt={}, &block)
  puts cmd unless opt[:silent]
  return if @opt.dry_run

  if block
    block.call(%x`#{cmd}`)
  else
    system cmd
  end
end
should_have_db(ver) click to toggle source
# File lib/myrurema.rb, line 202
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