Class: MyRurema

Inherits:
Object
  • Object
show all
Defined in:
lib/myrurema.rb,
lib/myrurema/options.rb,
lib/myrurema/version.rb

Defined Under Namespace

Classes: Options

Constant Summary collapse

SVN_URL =
"http://jp.rubyist.net/svn/rurema"
TMP_FILE =
Pathname(Dir.tmpdir)/'rurema_preview.html'
VERSION =
"0.3.5"

Instance Method Summary collapse

Constructor Details

#initialize(opt = Options.new(ARGV)) ⇒ MyRurema

Returns a new instance of MyRurema.



19
20
21
# File 'lib/myrurema.rb', line 19

def initialize(opt=Options.new(ARGV))
  @opt = opt
end

Instance Method Details

#initObject



49
50
51
52
53
# 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

#listObject



107
108
109
110
111
112
# 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

#previewObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# 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

#runObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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(query, ver) ⇒ Object

  • query: Array or String

  • ver: String



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/myrurema.rb', line 62

def search(query, ver)
  should_have_db(ver)

  args = Array(query).map{|s| Shellwords.escape s}.join(" ")
  cmd = "#{bitclust_path/'bin/refe'}" +
          " #{args} -d #{db_path(ver)}"
  sh cmd, :silent => true do |txt|
    lines = txt.lines.to_a
    if candidate_list_responded?(lines, query)
      words = {}
      k = 0
      puts lines.map{|line|
        line.gsub(/(\S+)/){|str|
          k+=1
          words[k] = str
          "(#{k})#{str}"
        }
      }
      print "which one? > "
      line = $stdin.gets or (puts; exit)
      n = line.to_i

      puts "searching #{words[n]}"
      puts
      search(words[n].sub(/\.#/, "."), ver)
    else
      puts txt
    end
  end
end

#search_num(query, num, ver) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/myrurema.rb', line 93

def search_num(query, num, ver)
  should_have_db(ver)

  result = `#{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

#serverObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# 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

#updateObject



55
56
57
58
# File 'lib/myrurema.rb', line 55

def update
  sh "svn up #{doctree_path}"
  refresh_db(@opt.rubyver)
end