Class: IHelp::IHelpDriver

Inherits:
RiDriver
  • Object
show all
Defined in:
lib/ihelp.rb

Overview

Version of RiDriver that takes its options as parameter to #initialize.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = []) ⇒ IHelpDriver

Create new IHelpDriver, with the given args passed to @options, which is a RI::Options.instance



498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/ihelp.rb', line 498

def initialize(args = [])
  @options = RI::Options.instance
  @options.parse(args)

  paths =  (if RUBY_VERSION > "1.8.4"
              @options.doc_dir
            else
              @options.paths
            end) || RI::Paths::PATH
  if paths.empty?
    report_missing_documentation(paths)
  end
  @ri_reader = RI::RiReader.new(RI::RiCache.new(paths))
  @display   = @options.displayer
end

Instance Attribute Details

#displayObject

Returns the value of attribute display.



493
494
495
# File 'lib/ihelp.rb', line 493

def display
  @display
end

#ri_readerObject

Returns the value of attribute ri_reader.



493
494
495
# File 'lib/ihelp.rb', line 493

def ri_reader
  @ri_reader
end

Instance Method Details

#display_info(info) ⇒ Object

Display the info based on if it’s for a class or a method. Using ri’s pager.



537
538
539
540
541
542
543
544
# File 'lib/ihelp.rb', line 537

def display_info(info)
  case [info.class] # only info.class doesn't work
  when [RI::ClassDescription]
    @display.display_class_info(info, @ri_reader)
  when [RI::MethodDescription]
    @display.display_method_info(info)
  end
end

#get_class_info_str(namespaces, klass_name) ⇒ Object

Get info for the class in the given namespaces.



548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/ihelp.rb', line 548

def get_class_info_str(namespaces, klass_name)
  return nil if namespaces.empty?
  klass_name_last = klass_name.split("::").last
  klass = nil
  namespaces.find{|ns|
    begin
      ns.name == klass_name_last and
      klass = @ri_reader.get_class(ns)
    rescue TypeError
      nil
    end
  }
  klass
end

#get_info_str(klass_name, method_name = nil, instance = false) ⇒ Object

Get info string from ri database for klass_name [method_name]



516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'lib/ihelp.rb', line 516

def get_info_str(klass_name, method_name = nil, instance = false)
  is_class_method = (not instance)
  top_level_namespace = @ri_reader.top_level_namespace
  namespaces = klass_name.split(/::/).inject(top_level_namespace){
    |ns, current_name|
    @ri_reader.lookup_namespace_in(current_name, ns)
  }
  return nil if namespaces.empty?
  if method_name.nil?
    get_class_info_str(namespaces, klass_name)
  else
    methods = @ri_reader.find_methods(
                method_name, is_class_method, namespaces)
    return nil if methods.empty?
    get_method_info_str(method_name, methods)
  end
end

#get_method_info_str(requested_method_name, methods) ⇒ Object

Get info for the method in the given methods.



565
566
567
568
569
570
571
572
573
574
575
# File 'lib/ihelp.rb', line 565

def get_method_info_str(requested_method_name, methods)
  if methods.size == 1
    @ri_reader.get_method(methods.first)
  else
    entries = methods.find_all {|m| m.name == requested_method_name}
    return nil if entries.empty?
    method = nil
    entries.find{|entry| method = @ri_reader.get_method(entry)}
    method
  end
end