Class: ORI::Library

Inherits:
Object show all
Defined in:
lib/ori/library.rb

Overview

ri lookup library.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Library

Returns a new instance of Library.



14
15
16
17
# File 'lib/ori/library.rb', line 14

def initialize(attrs = {})
  @cache = {}
  attrs.each {|k, v| send("#{k}=", v)}
end

Instance Attribute Details

#frontendObject

Mask of ri command to fetch content. Example:

ri -T -f ansi %s


9
10
11
# File 'lib/ori/library.rb', line 9

def frontend
  @frontend
end

#shell_escapeObject

Shell escape mode. :unix or :windows.



12
13
14
# File 'lib/ori/library.rb', line 12

def shell_escape
  @shell_escape
end

Instance Method Details

#lookup(topic) ⇒ Object

Lookup an article.

lookup("Kernel#puts")     # => content or nil.


22
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
48
49
50
51
# File 'lib/ori/library.rb', line 22

def lookup(topic)
  if @cache.has_key? topic
    @cache[topic]
  else
    require_frontend

    etopic = case @shell_escape
    when :unix
      Tools.shell_escape(topic)
    when :windows
      Tools.win_shell_escape(topic)
    else
      topic
    end

    cmd = @frontend % etopic
    ##p "cmd", cmd
    content = `#{cmd} 2>&1`
    ##p "content", content

    # NOTES:
    # * Windows' ri always returns 0 even if article is not found. Work around it with a hack.
    # * Unix's ri sometimes returns 0 when it offers suggestions. Try `ri Object#is_ax?`.
    @cache[topic] = if $?.exitstatus != 0 or content.lines.count < 4
      nil
    else
      content
    end
  end
end