Class: BitClust::Subcommands::LookupCommand

Inherits:
BitClust::Subcommand show all
Defined in:
lib/bitclust/subcommands/lookup_command.rb

Constant Summary collapse

TEMPLATE =
{
  :library => {
    :text => <<-End,
    :html => <<-End
     <dl>
     <dt>type</dt><dd>library</dd>
     <dt>name</dt><dd><%= entry.name %></dd>
     <dt>classes</dt><dd><%= entry.classes.map {|c| c.name }.sort.join(', ') %></dd>
     <dt>methods</dt><dd><%= entry.methods.map {|m| m.name }.sort.join(', ') %></dd>
     </dl>
     <%= compile_rd(entry.source) %>
     End
  },
  :class   => {
    :text => <<-End,
    :html => <<-End
     <dl>
     <dt>type</dt><dd>class</dd>
     <dt>name</dt><dd><%= entry.name %></dd>
     <dt>library</dt><dd><%= entry.library.name %></dd>
     <dt>singleton_methods</dt><dd><%= entry.singleton_methods.map {|m| m.name }.sort.join(', ') %></dd>
     <dt>instance_methods</dt><dd><%= entry.instance_methods.map {|m| m.name }.sort.join(', ') %></dd>
     </dl>
     <%= compile_rd(entry.source) %>
     End
  },
  :method  => {
    :text => <<-End,
    :html => <<-End
     <dl>
     <dt>type</dt><dd><%= entry.type %></dd>
     <dt>name</dt><dd><%= entry.name %></dd>
     <dt>names</dt><dd><%= entry.names.sort.join(', ') %></dd>
     <dt>visibility</dt><dd><%= entry.visibility %></dd>
     <dt>kind</dt><dd><%= entry.kind %></dd>
     <dt>library</dt><dd><%= entry.library.name %></dd>
     </dl>
     <%= compile_rd(entry.source) %>
     End
  },
  :function => {
    :text => <<-End,
    :html => <<-End
     <dl>
     <dt>kind</dt><dd><%= entry.kind %></dd>
     <dt>header</dt><dd><%= entry.header %></dd>
     <dt>filename</dt><dd><%= entry.filename %></dd>
     </dl>
     <%= compile_rd(entry.source) %>
     End
  }
}

Instance Method Summary collapse

Methods inherited from BitClust::Subcommand

#error, #help, #option_error, #srcdir_root

Constructor Details

#initializeLookupCommand

Returns a new instance of LookupCommand.


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bitclust/subcommands/lookup_command.rb', line 14

def initialize
  super
  @format = :text
  @type = nil
  @key = nil
  @parser.banner = "Usage: #{File.basename($0, '.*')} lookup (--library|--class|--method|--function) [--html] <key>"
  @parser.on('--library=NAME', 'Lookup library.') {|name|
    @type = :library
    @key = name
  }
  @parser.on('--class=NAME', 'Lookup class.') {|name|
    @type = :class
    @key = name
  }
  @parser.on('--method=NAME', 'Lookup method.') {|name|
    @type = :method
    @key = name
  }
  @parser.on('--function=NAME', 'Lookup function. (C API)') {|name|
    @type = :function
    @key = name
  }
  @parser.on('--html', 'Show result in HTML.') {
    @format = :html
  }
end

Instance Method Details

#compile_rd(src) ⇒ Object


166
167
168
169
170
171
# File 'lib/bitclust/subcommands/lookup_command.rb', line 166

def compile_rd(src)
  umap = URLMapper.new(:base_url => 'http://example.com',
                       :cgi_url  => 'http://example.com/view')
  compiler = RDCompiler.new(umap, 2)
  compiler.compile(src)
end

#exec(argv, options) ⇒ Object


51
52
53
54
55
# File 'lib/bitclust/subcommands/lookup_command.rb', line 51

def exec(argv, options)
  super
  entry = fetch_entry(@db, @type, @key)
  puts fill_template(get_template(@type, @format), entry)
end

#fetch_entry(db, type, key) ⇒ Object


57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bitclust/subcommands/lookup_command.rb', line 57

def fetch_entry(db, type, key)
  case type
  when :library
    db.fetch_library(key)
  when :class
    db.fetch_class(key)
  when :method
    db.fetch_method(MethodSpec.parse(key))
  when :function
    db.fetch_function(key)
  else
    raise "must not happen: #{type.inspect}"
  end
end

#fill_template(template, entry) ⇒ Object


72
73
74
# File 'lib/bitclust/subcommands/lookup_command.rb', line 72

def fill_template(template, entry)
  ERB.new(template).result(binding())
end

#get_template(type, format) ⇒ Object


76
77
78
79
# File 'lib/bitclust/subcommands/lookup_command.rb', line 76

def get_template(type, format)
  template = TEMPLATE[type][format]
  TextUtils.unindent_block(template.lines).join('')
end

#parse(argv) ⇒ Object


41
42
43
44
45
46
47
48
49
# File 'lib/bitclust/subcommands/lookup_command.rb', line 41

def parse(argv)
  super
  unless @type
    error "one of --library/--class/--method/--function is required"
  end
  unless argv.empty?
    error "too many arguments"
  end
end